AWK parsing top command output - awk

AWK code:
top -b|head -20|awk '/PID/,EOF {print $0}'|grep -v PID|while read line; do awk -v MYHOST=$(hostname) '{print "topstat,host="MYHOST",PID="$1" USER="$2",PR="$3",NI="$4",VIRT="$5",RES="$6",SHR="$7",STATE="$8",%CPU="$9",%MEM="$10",TIME="$11,COMMAND="$12}';done
The o/p line(1 line for e.g) is
topstat,host=host1.abc.com,PID=14 USER=root,PR=rt,NI=0,VIRT=0,RES=0,SHR=0,STATE=S,%CPU=0.0,%MEM=0.0,TIME=0:00.36,COMMAND=migration/1
I need the O/p
topstat,host=host1.abc.com,PID=14 USER=root,PR=rt,NI=0,VIRT=0,RES=0,SHR=0,STATE="S",%CPU=0.0,%MEM=0.0,TIME=0:00.36,COMMAND="migration/1"
Any help?

You need to use \ escape character
top -b|head -20|tail -n +6 | awk -v MYHOST=$(hostname) '{print "topstat,host="MYHOST",PID="$1" USER="$2",PR="$3",NI="$4",VIRT="$5",RES="$6",SHR="$7",STATE=\""$8"\",%CPU="$9",%MEM="$10",TIME="$11,COMMAND="$12}'

Related

Replace end of line with comma and put parenthesis in sed/awk

I am trying to process the contents of a file from this format:
this1,EUR
that2,USD
other3,GBP
to this format:
this1(EUR),that2(USD),other3(GBP)
The result should be a single line.
As of now I have come up with this circuit of commands that works fine:
cat myfile | sed -e 's/,/\(/g' | sed -e 's/$/\)/g' | tr '\n' , | awk '{print substr($0, 0, length($0)- 1)}'
Is there a simpler way to do the same by just an awk command?
Another awk:
$ awk -F, '{ printf "%s%s(%s)", c, $1, $2; c = ","} END { print ""}' file
1(EUR),2(USD),3(GBP)
Following awk may help you on same.
awk -F, '{val=val?val OFS $1"("$2")":$1"("$2")"} END{print val}' OFS=, Input_file
Toying around with separators and gsub:
$ awk 'BEGIN{RS="";ORS=")\n"}{gsub(/,/,"(");gsub(/\n/,"),")}1' file
this1(EUR),that2(USD),other3(GBP)
Explained:
$ awk '
BEGIN {
RS="" # record ends in an empty line, not newline
ORS=")\n" # the last )
}
{
gsub(/,/,"(") # replace commas with (
gsub(/\n/,"),") # and newlines with ),
}1' file # output
Using paste+sed
$ # paste -s will combine all input lines to single line
$ seq 3 | paste -sd,
1,2,3
$ paste -sd, ip.txt
this1,EUR,that2,USD,other3,GBP
$ # post processing to get desired format
$ paste -sd, ip.txt | sed -E 's/,([^,]*)(,?)/(\1)\2/g'
this1(EUR),that2(USD),other3(GBP)

Slow process in code

can you help to improve this code to be faster .. with 50000 lines in my file this take a lot time.
I appreciate your help
input
17/11/27 03:13:50:480000
17/11/27 03:12:54:380000
17/11/27 03:14:39:980000
output
1195787648480000
1195787592380000
1195787697980000
my code
ts=$(date -d'01/06/1980 00:00:00' +%s)
lap=18
cat file |
while read tt
do
dt=`echo $tt | awk '{print $1}' | awk -F"/" '{print $2"/"$3"/"$1}'`
tm=`echo $tt | awk '{print substr($2,1,8)}'`
ms=`echo $tt | awk '{print $2}' | awk -F":" '{print $NF}'`
line=`echo $dt" " $tm`
echo $line\ $(date -d "${line/// }" "+%s") |
awk '{print (($3 - '$ts') + '$lap')'$ms'}'
done
Please, help me to improve my code to get results faster.
Many thanks.
With single GNU awk process:
awk -F'[[:space:]]*|/|:' -v ts=$(date -d'01/06/1980 00:00:00' +%s) -v lap=18 '{
print (mktime(sprintf("20%d %d %d %d %d %d",$1,$2,$3,$4,$5,$6)) - ts)+lap $NF
}' file
The output:
1195791248480000
1195791192380000
1195791297980000
Enjoy )
similar with gawk
$ awk -F'[/: ]' -v ts=$(date -d'01/06/1980' +%s) \
-v lap=18 '{ms=$NF; $NF=""; d=sprintf(20$0);
print mktime(d)+lap-ts ms}' file
1195787648480000
1195787592380000
1195787697980000

AWK command for parameterize

I can run this:
awk -v DATE="$(date +"%d%m%Y")" 'BEGIN{print DATE }1''1; END{print "EOF"NR-1}' Assgmt_B1_v1_16032017.CSV > NoticesPrinting_v1_farah.csv
But after parameterize, I could not run anymore... please help me.
Today_Date=`date +"%d%m%Y"`
Current_Date=`date +"%d%m%Y"`
awk -v DATE="${Today_Date}" path="${Target_Dir}"'BEGIN{print DATE};END{print "EOF"NR-1}' "${Source_Dir}/${log}" > file= path ""NoticesPrinting_v1".csv"
Input:
Header 1|Header 2|Header 3
10000001|10000002|10000003
Output:
17032017
Header 1|Header 2|Header 3
10000001|10000002|10000003
EOF1
Syntax is wrong in your case
awk -v DATE="$(date +"%d%m%Y")" 'BEGIN{print DATE }1''1; END{print "EOF"NR-1}' Assgmt_B1_v1_16032017.CSV > NoticesPrinting_v1_farah.csv
^
Here unnecessary quote
Correct like this
awk -v DATE="$(date +"%d%m%Y")" 'BEGIN{print DATE }1; END{print "EOF"NR-1}' Assgmt_B1_v1_16032017.CSV > NoticesPrinting_v1_farah.csv
and
awk -v DATE="${Today_Date}" path="${Target_Dir}"'BEGIN{print DATE};END{print "EOF"NR-1}' "${Source_Dir}/${log}" > file= path ""NoticesPrinting_v1".csv"
^ ^
Missing -v Missing space ^
O/p redirection
Correct like below
awk -v DATE="${Today_Date}" -v path="${Target_Dir}" '
BEGIN{print DATE};
END{print "EOF"NR-1}
' "${Source_Dir}/${log}" > "NoticesPrinting_v1.csv"
and The backticks (`...`) is the legacy syntax required by only the very oldest of non-POSIX-compatible bourne-shells and $(...) is POSIX and more preferred, at least $(...) stands out visually better.
Today_Date=`date +"%d%m%Y"`
To
Today_Date=$(date +"%d%m%Y")
See : Why is $(...) preferred over (`...`) (backticks)?
Answer to Comment
the output is come out but only header and trailer...othr output
doesnot appear
$ cat f
Header 1|Header 2|Header 3
10000001|10000002|10000003
$ Today_Date=$(date +"%d%m%Y")
$ Target_Dir="something"
$ awk -v DATE="${Today_Date}" -v path="${Target_Dir}" '
BEGIN{print DATE}1;
END{print "EOF"NR-1}
' f
17032017
Header 1|Header 2|Header 3
10000001|10000002|10000003
EOF1

Using a variable as a operand in a expression - awk

I tried doing something like this in awk?
limit=10000
ls -ltr | awk '$5 >= $limit { print $5 }'
But it doesn't seem to work, it prints all size less than the limit too.
Thanks in advance!
Try to set variable in AWK's paramaters using -v option:
limit=10000
ls -ltr | awk -v mylimit=$limit '$5 >= mylimit {print $5}'
Change your awk command like below,
ls -ltr | awk -v limit=10000 '$5 >= limit { print $5 }'
you could declare an variable in awk itself using -v switch.

multiple field separator in awk

I'm trying to process an input which has two field seperators ; and space. I'm able to parse the input with one separator using:
echo "10.23;7.15;6.23" | awk -v OFMF="%0.2f" 'BEGIN{FS=OFS=";"} {print $1,$2,$3}'
10.23;7.15;6.23
For an input with two seperators, I tried this and it doesn't parse both the seperators:
echo "10.23;7.15 6.23" | awk -v OFMF="%0.2f" 'BEGIN{FS=OFS=";" || " "} {print $1,$2,$3}'
You want to set FS to a character list:
awk -F'[; ]' 'script' file
and the other builtin variable you're trying to set is named OFMT, not OFMF:
$ echo "10.23;7.15 6.23" | awk -F'[; ]' -v OFMT="%0.2f" '{print $1,$2,$3}'
10.23 7.15 6.23
$ echo "10.23;7.15 6.23" | awk 'BEGIN{FS="[; ]"; OFS=";"; OFMT="%0.2f"} {print $1,$2,$3}'
10.23;7.15;6.23