Jairaj
1
Hi All,
I want to do some calculation in 5th field and then replace the original 5th value to calcualted new value:
ifile
1|2|3|4|one two three|test
Expecting ofile
1|2|3|4|twothree|test
I tried below commands and acheived.
awk -F"|" '{print $5}' ifile | awk '{if (NF==3) {print $2$3} else if (NF==2) {print $2}}' > tempfile
paste -d "|" ifile tempfile | awk 'BEGIN {FS=OFS="|"} { $5=$NF;NF--;print }' > ofile
How can I acheive this with one awk command itself.
Thanks in advance.
Like this?
awk -F"|" '{ for(i=1;i<=NF;i++){t=gsub(/ /,"@",$i); if(t!=0){split($i,arr,"@"); printf arr[2] arr[3] FS}else{printf $i FS} }}' inputFile
--ahamed
danmero
3
awk -F\| '{x=split($5,a,OFS);for(i=1;++i<=x;){y=(y)?y a:a};sub($5,y)}1'
If is not what you want please elaborate.
awk -F\| '{split($5,F," ");$5=F[2](F[3]==x?x:F[3])}1' OFS=\| file
-or-
awk '{sub(/[^|]*$/,x,$1)}1' OFS= file
Jairaj
5
Thanks Ahmed & Danmero,
Both your commands are working.
for sample I given below calculation in 5th field
awk '{if (NF==3) {print $2$3} else if (NF==2) {print $2}}'
But I wanted to do calclulation dynamically. My aim is to do some calculation in 5field and replace by the same.
danmero
6
What is the logic for your calculation ? print only the last 2 words from $5
?
awk -F\| '{x=split($5,a,OFS);sub($5,a[(x-1)]a[x])}1'