Hi, I am trying to do something like this ...
I use awk to match a pattern, and then print out all col.
My code is :
awk '{if ($1 ==300) print $1,$2-'$sbin7',$3}' tmp.txt
output=
300 2
whereby sbin7=2,
The thing is, I want to print all col and row, not just the matched line/row only, but I still want to make the change once pattern match.
I want to print out something like this:
10 6
300 2
303 1
any chance ?
tmp.txt content:
10 6
300 4
303 1
anbu23
February 6, 2015, 5:53am
2
Can you add code tags?
awk '{ print $1,$2-($1 == 300 ? '$sbin7' : 0),$3}' tmp.txt
1 Like
awk -vsbin7="$sbin7" '{ print $1,$2-($1 == 300 ? sbin7 : 0),$3}' tmp.txt
OR
awk '{ print $1,$2-($1 == 300 ? sbin7 : 0),$3}' sbin7="$sbin7" tmp.txt
1 Like
RudiC
February 6, 2015, 8:33am
4
awk '$1 == 300 {$2-=sbin7} 1' sbin7="$sbin7" file
10 6
300 2
303 1
1 Like
Hi All,
I am trying out the above example, found that all is useful
At the end I use something like this
istwo=2
awk '$1 == 300 {$2-='$istwo'} 1' file
But my output become like this, which the line format is out from original format:
100 2 0.54
200 1 0.32
300 2 0.15
405 4 0.24
original file:
100 2 0.54
200 1 0.32
300 4 0.15
405 4 0.24
So, wonder how to retain the original format or close to it after apply the awk command above ? thks ! :>
any reason why you're not following the suggested solutions? What is it that din't work exactly as suggested?
I follow exactly using one of the example above.
Some examples generate even more out of format output from the file, although it is successfully matching and did the job. thks !
Try:
sbin7=2
awk -v n=300 -v corr="$sbin7" '$1==n{$2-=corr} {printf "%9d %5d %7.2f\n",$1,$2,$3}' file
Note that this adaptation is needed because file your format is different from the sample in post #1 .
This is why it is important to make your sample representative for the real data right from the start.
1 Like
Thanks all. Next time i will put the real copy of sample file(format) so that thing can be solved at one shot
It is working now.
working fine, but has complaint about the decimal point which is too long.
See the sample :
before:
Sort Total
Site Sort SortName Parts %/Total
----------------------------------------------
9 1 0.06
10 6 0.35
Using command :
awk '$1 == 10 {$2-=1;$3=$2/1720*100} 1' filename|sed 's/^10 / 10 /g'
result:
Sort Total
Site Sort SortName Parts %/Total
----------------------------------------------
9 1 0.06
10 5 0.290698
The thing is, I need the 0.290698 above to just have 2 decimal point, become --> 0.29
So I need advice ! Thkxxx !
Desire result:
Sort Total
Site Sort SortName Parts %/Total
----------------------------------------------
9 1 0.06
10 5 0.29
Moderator comments were removed during original forum migration.