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
Related
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}'
I am trying to store the Date value in the 3rd row, second column (always this position) in var using awk. The code executes and echo the current result, not the desired. If I understand the code the value in the 3rd row, second column is printed and stored in var. Thank you :).
file
[ID]
Sample,one,,,
Date,1/1/2015,,,
Test,xxx,,,
[Data]
....,,,
....,,,
desired
echo var
1/1/2015
current
echo var
one 1/1/2015 xxx
awk
var=$(awk -F, -v r=3 -v c=2 '{print $c}' file)
this program will get the output you need:
awk 'BEGIN{FS=","}/Date/ {print $2}' file
or:
awk -F, '/Date/ {print $2}' file
(option 01) the solution you need is:
var=$(awk -F, '/Date/ {print $2}' file)
echo "$var"
based in solution you proposed, you can print only the third row:
awk -F, -v r=3 -v c=2 '{if(NR==r)print $c}' file
(option 02)
var=$(awk -F, -v r=3 -v c=2 '{if(NR==r)print $c}' file)
echo "$var"
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)
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.
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