INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

creating fields with awk

creating fields with awk

(OP)
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?

RE: creating fields with awk

Hi

Quote (man awk)

       In the special
       case  that FS is a single space, fields are separated by runs of spaces
       and/or tabs and/or newlines.
 
(...)

FS          The input field separator, a space by default. 
So your multiple adjacent \t 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 --> command line

awk -F '\t' '{ sum += $1} END {print sum}' 

Feherke.
feherke.ga

RE: creating fields with awk

(OP)
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?

RE: creating fields with 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 --> command line

master # echo $'\t\t\t\t\n\t\t\t\t\n\t\t\t\t' > rog64

master # tr '\t' '|' < rog64 
||||
||||
||||

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

master # tr '\t' '|' < rog64 
||||
||42||
|||| 

Feherke.
feherke.ga

RE: creating fields with awk

Hi

By the way, for so extremely simple thing like replacing one field, Sed could be also enough :

CODE --> command line

master # echo $'\t\t\t\t\n\t\t\t\t\n\t\t\t\t' > rog64

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

master # tr '\t' '|' < rog64 
||||
||42||
|||| 

Feherke.
feherke.ga

RE: creating fields with awk

(OP)
Wow, great response and well explained, thanks alot, it really helped.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close