Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating fields with awk

Status
Not open for further replies.

rog64

Programmer
Mar 23, 2015
3
IE
Hi, i am writing a bash script and i am trying to redirect output to specific fields in a file. I cant seem to find any way of doing this. I have tried to create fields and add to a specific field using the following line
printf "%s\t%s\t%s\t%s\t%s\n" "" "" "$total" "" "" >> study.txt
which works, but when i try to retrieve info from a specific field using the following line:
cat study.txt | awk '{ sum += $1} END {print sum}'
it doesnt give me the sum of field 1, it gives the sum of all fields combined. Can you add to a specific field using awk?
 
Hi

man awk said:
[pre] In the special
case that FS is a single space, fields are separated by runs of spaces
and/or tabs and/or newlines.
[/pre](...)[pre]

FS The input field separator, a space by default.[/pre]
So your multiple adjacent [tt]\t[/tt] characters are counted as a single one, making the $total you output to be the 1st field, not the 3rd.

Not sure what is your ultimate goal, but looks like you need to explicitly specify the field separator :
Code:
awk [highlight]-F '\t'[/highlight] '[teal]{[/teal] sum [teal]+=[/teal] [navy]$1[/navy][teal]}[/teal] [b]END[/b] [teal]{[/teal][b]print[/b] sum[teal]}[/teal]'

Feherke.
feherke.ga
 
Thanks for the reply. I am not using the printf line i entered above. it does not work. I am trying to create five fields seperated by tabs, once this is done i need to enter info into each field seperately. Can this be done using awk?
 
Hi

AWK processes data as a stream. Although that stream can come from a file, AWK itself not handles it as file. From your last post I understand you want something like in-place editing of a file. AWK is not able to do that. The closest alternative would be Perl.

For example, having a file with 3 rows * 5 fields, this is how you place a value in 2nd row 3rd field :
Code:
[blue]master #[/blue] echo $'\t\t\t\t\n\t\t\t\t\n\t\t\t\t' > rog64

[blue]master #[/blue] tr '\t' '|' < rog64 
||||
||||
||||

[gray]#                      ,---------------------------------- input field separator[/gray]
[gray]#                      |         ,------------------------ field number, counted from 0[/gray]
[gray]#                      |         |  ,--------------------- new value[/gray]
[gray]#                      |         |  |       ,------------- row number, counted from 1[/gray]
[gray]#                      |         |  |       |          ,-- output field separator[/gray]
[blue]master #[/blue] perl -pi -aF'\t' -e '[navy]$F[/navy][teal][[/teal][purple]2[/purple][teal]]=[/teal][purple]42[/purple][b]if[/b][navy]$.[/navy][teal]==[/teal][purple]2[/purple][teal];[/teal][navy]$_[/navy][teal]=[/teal][b]join[/b][i][green]"\t"[/green][/i][teal],[/teal][navy]@F[/navy]' rog64

[blue]master #[/blue] tr '\t' '|' < rog64 
||||
||42||
||||

Feherke.
feherke.ga
 
Hi

By the way, for so extremely simple thing like replacing one field, Sed could be also enough :
Code:
[blue]master #[/blue] echo $'\t\t\t\t\n\t\t\t\t\n\t\t\t\t' > rog64

[gray]#                ,--------------- row number, counted from 1[/gray]
[gray]#                |     ,--------- field separator[/gray]
[gray]#                |     |   ,----- new value[/gray]
[gray]#                |     |   |  ,-- field number, counted from 1[/gray]
[blue]master #[/blue] sed -i '2s/[^\t]*/42/3' rog64 

[blue]master #[/blue] tr '\t' '|' < rog64 
||||
||42||
||||


Feherke.
feherke.ga
 
Wow, great response and well explained, thanks alot, it really helped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top