hello
I have a file with lines of info separated with "|"
I want to amend the second field of the last line, using AWK
my problem is with geting awk to return the last line
this is what I am using
awk 'END{ print $0 }' myFile
but I get an empty result
I tried the same thing with tail
tail -1 myFile
which works fine and returns the last line, however I need it to work with awk
Also tried print, print $1, print $* with no luck,
any suggestion would be much appriciated!
---------- Post updated at 03:59 PM ---------- Previous update was at 03:53 PM ----------
This is what I get
$ tail -1 myFile
251|21|FOOTER|FILE||22
$ awk 'END{print}' myFile
$
ctsgnb
March 14, 2011, 12:17pm
2
awk '{x=$0}END{print x}' myFile
or
sed '$!d' myFile
or
sed -n '$p' myFile
or
tail -1 myFile
I think it should be written like below:
awk -F"|" '{x=$0}END{print x}' myFile
ctsgnb
March 14, 2011, 12:35pm
4
Ooops ... i was focusing on getting the last line
nawk -F\| '{$2="something_else_modified";x=$0}END{print x}' OFS=\| infile
Sadly the awk way still returns empty
so i tried this:
var1=$(cat myFile | wc -l)
echo $var1
awk -F"|" '$1==var1 {$2=0; print >> "myFile"}' OFS="|" myFile
now I know that I cannot just use $ to pass shell variables into awk, and the above does not work, how can I pass var1 into the awk?
ideally this what I want to do, "on the last line, change the second field to 0"
awk -F\| 'END{print $2}' file
---------- Post updated at 12:43 PM ---------- Previous update was at 12:41 PM ----------
var=VAR
awk -F\| -vv=$var 'END{print $2, v}' file
danmero:
awk -F\| 'END{print $2}' file
---------- Post updated at 12:43 PM ---------- Previous update was at 12:41 PM ----------
var=VAR
awk -F\| -vv=$var 'END{print $2, v}' file
you got me very confused here...
this what I tried after your reply:
var1=$(cat myFile| wc -l)
echo $var1
awk -F"|" -v v="$var1" ' $1=v {$2=0; print >> "myFile"}' myFile
I dont just want to print the second field of the last line, I want to change it to 0, my problem is END does not work as for some reason it thinks the last line is empty
now what it does is changing everything to 0 infinetly...
ctsgnb
March 14, 2011, 1:06pm
8
/usr/xpg4/bin/awk -F\| '{$2=0;x=$0} END {print x}' OFS=\| myFile
If you are on solaris, use nawk instead of awk or use the posix awk which should be located in /usr/xpg4/bin/awk
# tail -1 infile
"CCCC"|"My Test C"|"Test "CCCC""|"This is C"
# r=REPLACE_STRING
# nawk -F\| -v V="$r" '{$2=V;x=$0}END{print x}' OFS=\| infile
"CCCC"|REPLACE_STRING|"Test "CCCC""|"This is C"
#
sadly I am not on solaris...
I am bumbed now, it keeps changing everything to 0 now...
This should be very simple, just go to the last line, change the second field to 0 save file, end of story, no weird sed, no solaris,
var1=$(cat SocaFileFixed_Final | wc -l)
echo $var1
awk -F"|" -v v="$var1" ' $1==v {$2=0}' myfile
can't see why this doesn't work :wall::wall::wall:
116
March 14, 2011, 1:25pm
10
Hey u are comparing $1 with the nof lines in the file. Does your $1 contain line numbers?
nawk 'FNR==1{p=$0;next}{print p;p=$0}END {$0=p;$2=0;print}' myFile
116
March 14, 2011, 1:34pm
12
Better try this one
var=$(cat filename|wc -l)
awk -F"|" -v var=$var ' NR==var {$2=0; print $0}'
Ok found it
heres the code that works as I want
rm temp3
var1=$(cat myFile | wc -l)
echo $var1
awk -F"|" -v v="$var1" -v c=0 ' $1==v{$2=0;print >>"temp3" ; prev=c ; c=c+1 ;next} {print >> "temp3"}' OFS="|" myFile
ctsgnb
March 14, 2011, 1:40pm
14
sed -i '$s:[^|]*|:0|:2' infile
# cat infile
"AAAA"|"Test "1-A""|"Test AAAA"|"This is A"
"BBBB"|"Test "1-B""|"Test BBBB"|"This is B"
"CCCC"|"My Test C"|"Test "CCCC""|"This is C"
# sed '$s:[^|]*|:0|:2' infile
"AAAA"|"Test "1-A""|"Test AAAA"|"This is A"
"BBBB"|"Test "1-B""|"Test BBBB"|"This is B"
"CCCC"|0|"Test "CCCC""|"This is C"
Which OS + version do you run ?