Find the answer to your Linux question:
Results 1 to 8 of 8
Hi Linux folks, I am a beginner of bash/ awk and face the following issue: ATOM 16054 O HOH W 1 28.905 -7.972 -29.142 ATOM 16410 O HOH W 1 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24

    awk script help


    Hi Linux folks,

    I am a beginner of bash/ awk and face the following issue:

    ATOM 16054 O HOH W 1 28.905 -7.972 -29.142
    ATOM 16410 O HOH W 1 51.525 -8.479 17.329
    ATOM 16797 O HOH W 1 -24.200 11.137 -38.335
    ATOM 16933 O HOH W 1 30.877 7.000 20.773

    We want to make the "1" in column six to be the same as line number. E.g. line one 1= "1"; line two 1= "2", line three 1= "3"; line four 1= "4" . I write the following script

    cat ${filename} | awk '{sub(/[0-9]+/,"'"$NR"'",$6); print}' > ${outputfile}.txt

    However, the script never replaces the "1" in the column six.

    The output file is like this

    ATOM 16054 O HOH W 28.905 -7.972 -29.142
    ATOM 16410 O HOH W 51.525 -8.479 17.329
    ATOM 16797 O HOH W -24.200 11.137 -38.335
    ATOM 16933 O HOH W 30.877 7.000 20.773


    Could anyone kindly offer any solution? Thank you!

    Henry

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hi,

    You don't need to use "$" to use an awk variable. Try this:

    Code:
    cat filename |awk '{ $6 = NR;print }'

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24
    Hi atreyu,

    Thank you so much for your remind. Your one is a lot easier. However, after using the cat filename |awk '{ $6 = NR;print }'; the original alignment is lost.

    Here is the original alignment:

    ATOM 16054 O HOH W 1 28.905 -7.972 -29.142 1.00 9.91 W O
    ATOM 16410 O HOH W 1 51.525 -8.479 17.329 1.00 28.17 W O
    ATOM 16797 O HOH W 1 -24.200 11.137 -38.335 1.00 20.47 W O

    Here is the corrected one with wrong alignment.

    ATOM 16054 O HOH W 1 28.905 -7.972 -29.142 1.00 9.91 W O
    ATOM 16410 O HOH W 2 51.525 -8.479 17.329 1.00 28.17 W O
    ATOM 16797 O HOH W 3 -24.200 11.137 -38.335 1.00 20.47 W O

    I write something like this: "awk '{$6 = NR} {printf "%4s %7i %3s %6s %2s %4s %8.3f %4.3f %4.3f %3.2f %3.2f %7s %5s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' ${filename} > ${outputfile}.txt"

    Is there any way to shorten it? Thanks!

    Henry

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by psu4 View Post
    I write something like this: "awk '{$6 = NR} {printf "%4s %7i %3s %6s %2s %4s %8.3f %4.3f %4.3f %3.2f %3.2f %7s %5s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' ${filename} > ${outputfile}.txt"

    Is there any way to shorten it?
    Probably, but I'm no awk wizard. My first inclination would have been resorting to printf, like you did.

  6. #5
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24
    One more question,

    After I use the above command ""awk '{$6 = NR} {printf "%4s %7i %3s %6s %2s %4s %8.3f %4.3f %4.3f %3.2f %3.2f %7s %5s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' ${filename} > ${outputfile}.txt"", the result is followed: the last two columns cannot align very well. The reason seems to be the negative float numbers (%8.3f %4.3f %4.3f => $7,$8,$9).

    ATOM 16054 O HOH W 1 28.905 -7.972 -29.142 1.00 9.91 W O
    ATOM 16410 O HOH W 2 51.525 -8.479 17.329 1.00 28.17 W O
    ATOM 16797 O HOH W 3 -24.200 11.137 -38.335 1.00 20.47 W O
    ATOM 16933 O HOH W 4 30.877 7.000 20.773 1.00 25.66 W O
    ATOM 16985 O HOH W 5 -7.762 21.829 26.450 1.00 26.81 W O
    ATOM 16055 O HOH W 6 5.736 -0.373 32.953 1.00 6.29 W O
    ATOM 16411 O HOH W 7 25.908 8.279 -16.942 1.00 23.18 W O

    This should be the correct alignment:

    ATOM 16054 O HOH W 1 28.905 -7.972 -29.142 1.00 9.91 W O
    ATOM 16410 O HOH W 2 51.525 -8.479 17.329 1.00 28.17 W O
    ATOM 16797 O HOH W 3 -24.200 11.137 -38.335 1.00 20.47 W O

    Please give me some comments. Thanks.

    Henry

  7. #6
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    204
    First of all, I can't see the alignment problems you're talking about from your comments. It suspect that because the forum software isn't presenting them using a fixed width font. So I think you'll have better luck showing us what you're seeing if you cut-n-paste the input and output using the "code" tags around the info.

    But in general if you specify a format that's not long big enough to handle the data, awk will expand the field width as needed. So if the format is "%4.3f" and the variable in question is >9 (or <0) then it won't fit. And awk will effectively use "%5.3f" for values in the 10-99 range, "%6.3f" for values in the "100-999" range, etc... Negative numbers need an extra character for the sign, so awk will effectively use "%5.3f" for values greater then -10, and "%7.3f" for values greater then -100 (meaning -99.999...) but less then -10.

    As for shortening the script to get rid of all the obnoxious formatting controls on "printf", here's an option... If the data before the 6th parameter is consistent enough that you can come up with one (or a small number) of string patterns, you couldO HOH W 1 try something like this.

    Code:
    seq = sprintf( "%3d", NR);
    sub( "O HOH W 1", "O HOH W" seq);
    print;
    But it assumes there's some constant text you can use to match and switch the "1".

  8. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,303
    Hi.
    Quote Originally Posted by psu4 View Post
    ... However, after using the cat filename |awk '{ $6 = NR;print }'; the original alignment is lost.
    ... Is there any way to shorten it? ...
    See post # 2 in thread http://www.linuxforums.org/forum/pro...ble-sizes.html for a pointer to a general, automatic field alignment utility: align

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  9. #8
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24
    Thanks everyone's solution. Learn a lot from them!! I finally solve the alignment problem by slightly modifying the original way " awk '{$6 = NR} {printf "%4s %7i %3s %6s %2s %4s %8.3f %4.3f %4.3f %3.2f %3.2f %7s %5s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' ${filename} > ${outputfile}.txt".

    Henry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •