Good day all,
I am running below command:
netstat -an | awk '/:25/{ print $4 }' | sed 's/:25//' | paste -sd ',' -
which produces
192.168.2.22,127.0.0.1
I would like to amend the result to something like below (to be parsed as a csv by an application)
Manuallyaddedtext 192.168.2.22,127.0.0.1
Many thanks
echo -n "Mytext " ; netstat...
Related
I am trying this below command:
cat dcl1serrfip_check.csv | grep -Fi 'BANK0_F5_WRDAT_P0[0]' | grep -i setup | grep 'L2H' | grep highv | grep -i low | awk -F ',' -v dev="0.861" -v rc="1.105" -v inte="0.872" '{ print ($10+$11)-(($12+$13)-($14))","($10*dev)+($11*rc)-(($12*dev)+($13*rc)-($14*inte))}'
This gives below output:
-6.93889e-18,0.000288
I want this output to be formatted to 4 decimal places. How to do it? The desired output would be
-0.0000,0.0002
You need, %0.4f or %.4f
To Test use :
awk 'BEGIN{ printf("%0.4f\n", -6.93889e-18) }'
So it becomes:
printf("%0.4f,%0.4f\n", ($10+$11)-(($12+$13)-($14)), ($10*dev)+($11*rc)-(($12*dev)+($13*rc)-($14*inte)) )
Actually you can rewrite your command in awk itself, no need of so many grep and cat combination
cat test.txt
serverabc.test.net
serverabc.qa.net
serverabc01.test.net
serverstag.staging.net
serverstag.test.net
here i need to match the duplicate strings just before the delimiter '.'
So the expected output would be like below. because string "serverabc" and "serverstag" found to be duplicates. Please help.
serverabc.test.net
serverabc.qa.net
serverstag.staging.net
serverstag.test.net
awk to the rescue!
$ awk -F\. '{c[$1]++; a[$1]=a[$1]?a[$1]RS$0:$0}
END{for(k in c) if(c[k]>1) print a[k]}' file
serverabc.test.net
serverabc.qa.net
serverstag.staging.net
serverstag.test.net
If it is not going to be used allot I would probably just do something like this:
cut -f1 -d\. foo.txt | sort |uniq -c | grep -v " 1 " | cut -c 9-|sed 's/\(.*\)/^\1\\./' > dup.host
grep -f dup.host foo.txt
serverabc.test.net
serverabc.qa.net
serverstag.staging.net
serverstag.test.net
test file contains
$ cat test
i-d119c118,vol-37905322,,,2015-07-29T03:50:32.511Z,General Purpose SSD,15
i-2278b42e,vol-c90539cc,,,2014-11-12T04:27:22.618Z,General Purpose SSD,10
script output:
$ for instance_id in $(cut -d"," -f1 test); do python getattrib.py get $instance_id | cut -d"'" -f2; done
10.10.0.68
10.10.0.96
inserting variable using sed yields following result, note the same IP address
$ insert=( `for instance_id in $(cut -d"," -f1 test); do python getattrib.py get $instance_id | cut -d"'" -f2; done` )
$ sed "s|$|,${insert}|" test
i-d119c118,vol-37905322,,,2015-07-29T03:50:32.511Z,General Purpose SSD,15,10.10.0.68
i-2278b42e,vol-c90539cc,,,2014-11-12T04:27:22.618Z,General Purpose SSD,10,10.10.0.68
but i am looking for output as below:
10.10.0.68,i-d119c118,vol-37905322,,,2015-07-29T03:50:32.511Z,General Purpose SSD,15
10.10.0.96,i-2278b42e,vol-c90539cc,,,2014-11-12T04:27:22.618Z,General Purpose SSD,10
use start delimiter ^ instead of end $ and adapt the ,
sed "s/^/${insert},/" test
but your sed and value retreiving need to be into the loop, not after or taking all result as value
example in loop:
for instance_id in $(cut -d"," -f1 test)
do
insert="$( python getattrib.py get ${instance_id} | cut -d"'" -f2 )"
sed -e "/^${instance_id}/ !d" -e "s|$|,${insert}|" test
done
insert=( `for instance_id in $(cut -d"," -f1 test); do python getattrib.py get $instance_id | cut -d"'" -f2; done` )
the insert variable is an array holding 2 elements
sed "s|$|,${insert}|" test
${insert} only retrieves the first element -- it is implicitly ${insert[0]}
I would rewrite that like this, to read the file line-by-line:
while IFS=, read -ra fields; do
ip=$( python getattrib.py get "${fields[0]}" | cut -d"'" -f2 )
printf "%s" "$ip"
printf ",%s" "${fields[#]}"
echo
done < test
I have an awk command to extract information from mount points (see the accepted answer in How to extract NFS information from mount on Linux and Solaris?):
awk -F'[: ]' '{if(/^\//)print $3,$4,$1;else print $1,$2,$4}
I would like to include a dig lookup in this awk command to lookup the IP of hostnames. Unfortunately, the mount command sometimes include an IP and sometimes a hostname. I tried the following, but it has an unwanted newline, unwanted return code and does not work if there is an IP address:
For hostnames
echo "example.com:/remote/export on /local/mountpoint otherstuff" | awk -F'[: ]' '{if(/^\//)print system("dig +short " $3),$4,$1;else print system("dig +short " $1),$2,$4}'
Returns
93.184.216.119
0 /remote/export /local/mountpoint
For IPs
echo "93.184.216.119:/remote/export on /local/mountpoint otherstuff" | awk -F'[: ]' '{if(/^\//)print system("dig +short " $3),$4,$1;else print system("dig +short " $1),$2,$4}'
Returns
0 /remote/export /local/mountpoint
I would like to retrieve the following in both cases
93.184.216.119 /remote/export /local/mountpoint
Update:
It seems that some versions of dig return the IP when an IP is provided as query and others return nothing.
Solution:
Based on the accepted answer I used the following adapted awk command:
awk -F'[: ]' '{if(/^\//) { system("dig +short "$3" | grep . || echo "$3" | tr -d \"\n\""); print "",$4,$1 } else { system("dig +short "$1" | grep . || echo "$1" | tr -d \"\n\"");print "",$2,$4 };}'
The additional grep . || echo "$3" takes care that the input IP/hostname is returned if dig returns nothing.
The system command in awk executes a command returns its status. Consider this:
$ awk 'END { print "today is " system("date") " and sunny" }' < /dev/null
Tue Jan 7 20:19:28 CET 2014
today is 0 and sunny
The date command outputs the date and a newline. When running from awk the same thing happens. In this example the system finishes before printf itself, so first we see the line with date, and on the next line our text with the return value 0 of system.
To get what we want we need to split this into multiple commands and we don't need the return value of system:
$ awk 'END { printf "today is "; system("date | tr -d \"\n\""); print " and sunny" }' < /dev/null
today is Tue Jan 7 20:24:01 CET 2014 and sunny
To prevent the newline after date, we piped its output to tr -d "\n".
Long story short, change from this:
print system(...), $2, $4
to this:
system(... | tr -d \"\n\"); print "", $2, $4
I have a document containing several lines of text.
Example(not actual):
*Prepare 42 Locked delete from table where type='test' and user_id='099'and number='+66719919*
I want to be able to search for user_id where ever it occurs in the document (which does not follow a pattern) and have the output as:
user_id=009
OR
009
Please how do I achieve this using awk?
Thanks.
awk '/user_id/{for(i=1;i<=NF;i++){if($i~/user_id/){split($i,a,"=");print a[2]}}}' your_file
tested:
> echo "*Prepare type='test' and user_id='099' and number='+66719919*" | awk '/user_id/{for(i=1;i<=NF;i++){if($i~/user_id/){split($i,a,"=");print a[2]}}}'
'099'
another one:
> echo "*Prepare type='test' and user_id='099' and number='+66719919*" | awk '/user_id/{for(i=1;i<=NF;i++){if($i~/user_id/){ print $i}}}'
user_id='099'
You could also use grep:
grep -o "user_id='\?[0-9]*'\?"
Append tr to remove the quotes:
grep -o "user_id='\?[0-9]*'\?" | tr -d \'