Help with AWK and Scripting!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with AWK and Scripting!
# 15  
Old 08-20-2010
Quote:
Originally Posted by SriJit
For the above, the input had in the first row of the matrix as two lines. According to the code, it reads the second line too and writes it to the output file in the same line. But it inserts another 0.00 when it reads the the beginning of the second line. I have highlighted it above.
How should I modify the code?
There is probably a trailing blank, or blanks on the first line. Initially I was deleting all trailing blanks, but that wouldn't work if the las 'value' on the line was really a blank set intended to be replaced with 0.00.

It could also be the way the data is aligned on the second row. If there is an extra space ahead of the data on the second line, that'd be a problem as well.

If either of these two problems is the cause, and its a single space that's causing the grief, then modify this line to have 4 spaces before the + instead of 2. If that doesn't work, then please post the first few lines, and it is important that you put them inside code tags to preserve the spacing.

Code:
gsub( "    +", " 0.00 ", buffer );   # 4 or more spaces causes 0.00 to insert

# 16  
Old 08-21-2010
Input to my code

Hi,
Here is how my input looks like.

Code:
Structure:                       RMSD RMSD  (Top-Right=Heavy atom alignment, Bottom-Left=Backbone atom alignment)

1E4P_0020_CCGA_PPNN_A_17_20.pdb  2.83 1.79  [     2.90 2.83 2.10 2.14 2.03 2.00 3.96 3.97 2.07 4.05 1.95 2.05 2.13 1.93 4.04 3.83 3.91 2.49 2.11 3.68 2.88 3.08 3.09 2.73 2.86 3.08 2.88 2.93 2.87 
                                             2.80 2.96 3.25 2.94 2.93 3.05 2.09 2.64 2.64 2.61]
1EHT_0002_CCGA_PPNN_A_12_15.pdb  3.33 2.14  [2.45      0.98 3.56 3.58 3.50 3.50 4.15 4.17 3.44 4.09 3.29 3.39 3.48 3.49 4.51 4.52 4.40 3.81 3.50 3.67 3.19 3.21 3.71 3.02 3.41 3.40 3.36 3.29 3.39 
                                             3.37 3.67 4.02 3.51 3.57 3.56 3.46 0.90 0.89 0.97]
1EHT_0007_CCGA_PPNN_A_12_15.pdb  3.37 1.97  [2.22 0.84      3.49 3.47 3.41 3.39 4.30 4.32 3.44 4.23 3.20 3.37 3.33 3.35 4.77 4.77 4.66 3.63 3.45 3.63 3.20 3.23 3.77 2.90 3.43 3.47 3.40 3.34 3.40 
                                             3.35 3.80 4.10 3.57 3.59 3.60 3.45 1.30 1.26 1.21]
1LDZ_0002_CCGA_PPNN_A_5_8.pdb    2.55 1.60  [1.17 2.20 2.03      0.45 0.49 0.55 3.31 3.26 0.88 3.47 0.79 1.09 0.67 0.81 3.29 3.07 3.25 0.89 0.71 3.58 3.37 3.58 3.10 3.46 3.15 3.35 3.22 3.40 3.18 
                                             3.18 3.09 3.10 3.21 2.97 3.21 0.80 3.50 3.47 3.45]


Please let me know how I should modify the code.

Thanks,
Srijit
# 17  
Old 08-21-2010
Thanks for posting the sample data -- very useful.

Small change. I was inserting an extra blank when joining multiple lines and that was causing the problem. Not sure why my test wasn't showing that, or maybe I wasn't looking closely enough.

Statements in bold were changed.
Code:
        awk '
        BEGIN {printf( "[" );   }               # opening bracket

        /]/  {
                if( last )
                        printf( "%s\n",  last );  # print the last one we saw

                if( partial )                           # add current line to partial buffer

                        buffer = partial substr( $0, indent );  # ditch leading spaces, but dont trash the "blank == 0" 
                else
                        buffer = $0;                    # no partial, just use current line

                gsub( ".*\\[", "[", buffer );      # trash all before [, but keep [
                gsub( "].*", "]", buffer );        # trash all after ], but keep ]
                gsub( "\\[", "", buffer );         # ditch opening bracket
                gsub( "]", "", buffer );           # ditch trailing bracket
                gsub( "  +", " 0.00 ", buffer );   # two or more spaces causes 0.00 to insert
                gsub( "  ", " ", buffer );         # cleanup if multiple spaces
                gsub( " $", "", buffer );          # cleanup trailing space if there
                gsub( "^ ", "", buffer );          # cleanup leading space if there

                last = buffer;               # save to add trailing ] if this is the last one
                join = 0;
                partial = "";

                next;
        }

        /\[/ {                                  # beginning of matrix, but not end
                indent = index( $0, "[" ) + 1;  # number of spaces to skip for secondary lines
                gsub( "^.*\\[", "[", $0 );      # ditch beginning junk
                partial = $0;                   # start a partial buffer
                join = 1;                       # join next line(s) if not end of matrix
                next;
        }

        join == 1 {
                buffer = substr( $0, indent )   # ditch leading spaces, but dont trash the "blank == 0"

                partial = partial buffer;       # add this line to the partial matrix (changed)
                next;
        }

        END {
                printf( "%s]\n", last );
        }
        '

Maybe this time! Have a great day.
# 18  
Old 08-23-2010
Works!

It works fine.

Thanks,
Srijit
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

New at scripting awk with variable

I am trying to write a shell script that will add a date to and awk command from the command prompt. here is the line I am having difficulties. zgrep -i playback $dir/$1-*-errors.out.gz |cut -d '|' -f 1,11,12,15 | awk -v start=${start} -v end=${end} -F '|' '{$1>=start && $1 <=end} {print $2... (7 Replies)
Discussion started by: infinity0880
7 Replies

2. Shell Programming and Scripting

awk Scripting

Hey guys, I want to get all the columns in this input file tab-delimited, i need to get the column send them to a variable. From there i could print them in shuffle and pick and select columns i want. Here is the input sample 2013/08/05 06:50:38:067 MINOR SiteScope ... (9 Replies)
Discussion started by: ryandegreat25
9 Replies

3. Shell Programming and Scripting

Need help in awk scripting

Hi I am beginner of shell/AWK scripting , can you please help me in select particular column and column between two pattern from a multiple column file. file1.txt number status date1 date2 description category ... (7 Replies)
Discussion started by: vijay_rajni
7 Replies

4. Shell Programming and Scripting

Need help with awk scripting

hi all, I am working on awk scripting.I have created two awk files and now have a requirement of replacing the contents of first file with some contents of second file. Please find below the two files created.File1 has 3 records and File2 has 4 records. cat File1 111,0165,CB21031251,0165,... (3 Replies)
Discussion started by: csrohit
3 Replies

5. Shell Programming and Scripting

Need help with awk scripting.

Hi, i am newbie to this site and hope to learn but problem is s but need help urgently. Plz pm me if you are good at this. Help will be appreciated. (11 Replies)
Discussion started by: Rookie80
11 Replies

6. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

7. Shell Programming and Scripting

awk scripting

Hi I have 2 files of large size( 10 Miilions) , i want to join 2 files based on some condition . for this taking lot of time and 100 cpu .i want to iterate the based on some value (like 1 lakh) I put the 2 files in the associative arrays . if the array limit reaches the 1 lach join the with... (2 Replies)
Discussion started by: kiranmosarla
2 Replies

8. Shell Programming and Scripting

Scripting via awk

Hi, I am trying to understand what is happening here, new to scripting: I have a couple of these, but if I knew what was going on in one I can figure out the rest: awk '/rpc-100083/ { $2 = "enable -r" } $3 ~ /.NOS99dtlogin/ { $t = $2; $2 = $3; $3 = $t } { print }' /var/svc/profile/upgrade... (2 Replies)
Discussion started by: ulemsee
2 Replies

9. Shell Programming and Scripting

AWK scripting

I have a text file in which the text has been divided into paragraphs (two line breaks or tab marks a new paragraph) and I want to make a script which output would delete line breaks within the paragraph and the different paragraphs would be separated by two line breaks. So, if my input file... (14 Replies)
Discussion started by: Muki101
14 Replies

10. UNIX for Dummies Questions & Answers

Awk scripting

Hi, I'm new to unix and i am kind of familiar with the basic commands. can anyone suggest some good books especially for AWK scripting and SHELL scripting thanks, Hari (2 Replies)
Discussion started by: rharee
2 Replies
Login or Register to Ask a Question