I have a file containing:
abc=1234.121
How to get only the numerical part?
1234.121
You can try this sed
sed 's/.*=//g'
You could use cut,
echo abc=1234.121 | cut -f2 -d'='|cut -f1 -d'.'
Here are some awk version:
echo "abc=1234.121" | awk -F= '$0=$2'
1234.121
echo "abc=1234.121" | awk -F= '{print $2}'
1234.121
Related
This is the command what i run.
ldap="$(ldapwhoami -x -H ldap://ABC.example.org -D "$user" -w "$pass")"
This is the output result:
u:ABC\1234567
May i know how to get the expected output ? like this 1234567
Thanks
1st Solution: Could you please try following.
echo "u:ABC\1234567" | awk -F'\' '{print $NF}'
OR
your_command | awk -F'\' '{print $NF}'
2nd solution: using awk's sub method.
your_command | awk '{sub(/.*\\/,"")} 1'
echo "u:ABC\1234567" | sed "s/[^0-9]//g"
There is a way with SED.
I have a variable with value like:
#capability_ids type="list">[LOADBALANCER]</capability_ids>#
And need to extract from this string type of equipment ( LOADBALANCER ).
I've tried to use cut, but don't know how write cut command with different delimiters.
DeviceType=$( echo $DeviceTypeDirty | cut -d'[' -f1)
Can enywone help me with right solution on bash?
use awk with regular expression: awk -F '[\\[\\]]' '{print $2}'
$ echo '#capability_ids type="list">[L3SWITCH]/capability_ids>#'|awk -F '[\\[\\]]' '{print $2}'
$ L3SWITCH
$ DeviceType=$( echo "$DeviceTypeDirty" | awk -F '[\\[\\]]' '{print $2}')
I tried and got to extract "LOADBALANCER"
Administrators-MacBook-Pro:~$ echo "\"list\">[LOADBALANCER]
</capability_ids>#"|awk -F '[][]' '{print $2}'
LOADBALANCER
Administrators-MacBook-Pro:~$
Hope that helps!
Using cut:
DeviceTypeDirty="#capability_ids type="list">[LOADBALANCER]</capability_ids>#"
DeviceType="$(echo "$DeviceTypeDirty" | cut -d'[' -f2 | cut -d']' -f1)"
Output:
echo "$DeviceType"
LOADBALANCER
I'm new to shell scripting. I need to get the value "test" from the below absolute path and print it. How can this be achieved using SED command. Please help.
/home/path/test_script/logs
In my opinion awk performs better this task. Using -F you can use multiple delimiters such as "/" and "_":
echo /home/path/test_script/logs | awk -F'/|_' '{print $4}'
sed code to first remove the _ and what's to the right of it, then the /s and what's to the left of those, leaving only "test".
echo /home/path/test_script/logs | sed 's/[_].*//;s,.*/,,g'
Output:
test
You can try awk command:
echo /home/path/test_script/logs | awk -F"/" '{print $4}' | cut -c1-4
Your output should be 'test'.
You can also assign the 'test' value to a variable by doing the below:
var1=`echo /home/path/test_script/logs | awk -F"/" '{print $4}' | cut -c1-4`
I have the following piece of code and would like to display HOST and RESULT side by side with a : separating them.
HOST=`grep pers results.txt | cut -d':' -f2 | awk '{print $1}'`
RESULT=`grep cleanup results.txt | cut -d':' -f2 | awk '{print $1}' | sed -e 's/K/000/' -'s/M/000000/'`
echo ${HOST}${RESULT}
Please can anyone assist with the final command to display these, I am just getting all of hosts and then all of results.
You probably want this:
HOST=( `grep pers results.txt | cut -d':' -f2 | awk '{ print $1 }'` ) #keep the output of the command in an array
RESULT=( `grep cleanup results.txt | cut -d':' -f2 | awk '{ print $1 }' | sed -e 's/K/000/' -'s/M/000000/'` )
for i in "${!HOST[#]}"; do
echo "${HOST[$i]}:${RESULT[$i]}"
done
A version that works without arrays, using an extra file handle to read from 2 sources at at time.
while read host; read result <&3; do
echo "$host:$result"
done < <( grep peers results.txt | cut -d: -f2 | awk '{print $1}' ) \
3< <( grep cleanup results.txt | cut -d':' -f2 | awk '{print $1}' | sed -e 's/K/000/' -'s/M/000000/')
It's still not quite POSIX, as it requires process substitution. You could instead use explicit fifes. (Also, an attempt to shorten the pipelines that produce the hosts and results. It's probably possible to combine this into a single awk command, since you can either do the substitution in awk, or pipe to sed from within awk. But this is all off-topic, so I leave it as an exercise to the reader.)
mkfifo hostsrc
mkfifo resultsrc
awk -F: '/peers/ {split($2, a, ' '); print a[1]}' results.txt > hostsrc &
awk -F: '/cleanup/ {split($2, a, ' '); print a[1]}' results.txt | sed -e 's/K/000' -e 's/M/000000/' > resultsrc &
while read host; read result <&3; do
echo "$host:$result"
done < hostsrc 3< resultsrc
Consider this ldd output for my executable:
linux-gate.so.1 => (0x003a5000)
libtcmalloc_minimal.so.0 => ./libtcmalloc_minimal.so.0 (0x00710000)
libBIBusTK.so => ./libBIBusTK.so (0x00b68000)
libCCLIDOM.so => ./libCCLIDOM.so (0x00a08000)
libxcb.so.1 => /usr/lib/libxcb.so.1 (0x00357000)
Here my interesting part is just this substring in each line:
./libtcmalloc_minimal.so.0
./libBIBusTK.so
./libCCLIDOM.so
/usr/lib/libxcb.so.1
Therefore I am using awk to achieve this, first I tried:
ldd ./BIBusTKServerMain | awk -F "=>" '{print $2}'
which gives me all the substring after "=>" symbol.
Then I tried one more step:
ldd ./BIBusTKServerMain | awk -F "=>" '{print $2}' | awk -F "(0x" '{print $1}'
which throws an error:
awk: fatal: Unmatched ( or \(: /(0x/
Note that if I tried this:
ldd ./BIBusTKServerMain | awk -F "=>" '{print $2}' | awk -F "0x" '{print $1}'
It produces:
./libtcmalloc_minimal.so.0 (
./libBIBusTK.so (
./libCCLIDOM.so (
/usr/lib/libxcb.so.1 (
What was wrong in my origial awk script?
I have also tried the sed alternative:
sed -n '/=>/,/\(0x/p' lddInfo.txt
Doesn't work either...
Following sed should work:
ldd ./BIBusTKServerMain | sed 's/^.* => \([^ ]*\) .*$/\1/'
Or this awk can also work:
ldd ./BIBusTKServerMain | awk '$3 ~ /^\.?\// {print $3}'
Why dont you simply try :
awk '{print $3}'
what is the reason behind not using this?
ldd ./BIBusTKServerMain | awk '{print $3}'
or you can also use:
perl -lne 'print $1 if(/=\> ([^\s]*)\s/)'
This works here (GnuSed under windows so the quotes need changing)
sed "s/ .* \(.*\) .*/\1/" test.txt