Sponsored Content
Top Forums Shell Programming and Scripting Line up Print outs from Separate Rows Post 302904966 by newageBATMAN on Sunday 8th of June 2014 02:24:51 PM
Old 06-08-2014
Quote:
Originally Posted by RudiC
Code:
awk     '/^Event/               {L=1}
         !L                     {next}
         /^Event/,/^END/        {REP[$1]=$2 "\t" $3; next}
         /Durat/||NF==0         {next}
         /^END/ && L            {print ""; exit}
         /^Date/,/^END/         {if (NF > 2)    {printf "\n"
                                                 printf "%s\t%s", $0, REP[$5]}
                                 if ($1 == "Date") printf "\tDuration\tClosed"
                                 if ($NF ~ /^(Y|N)$/)  printf "%s", $0}
        ' test.txt test.txt
Date    Month    Year    Time    Event    Description    Remarks    Duration    Closed
9        JUN        2013    1932    2    Reset    YYYYY
10        JAN        2014    1000    4    Unspecified    YYYYY        50            Y
11        APR        2013    1230    2    Reset    YYYYY
12        FEB        2014    1142    4    Unspecified    YYYYY
13        JAN        2015    0132    3    Reboot    ZZZZZ                          N
11        FEB        2012    1032    3    Reboot    ZZZZZ

I have slightly modified the Code to stream line the tabs for Formatted Output....
Code:
awk     '/^Event/               {L=1}
         !L                     {next}
         /^Event/,/^END/        {REP[$1]=$0; next}
         /Durat/||NF==0         {next}
         /^END/ && L            {print ""; exit}
         /^Date/,/^END/         {if (NF > 2)    {printf "\n"
                                                 printf "%5s %5s %5s %5s %5s", $1,$2,$3,$4, REP[$5]}
                                 if ($1 == "Date") printf "\tDuration\tClosed"
                                 if ($NF ~ /^(Y|N)$/)  printf "%s", $0}
        ' test.txt test.txt

and now the Output looks as follows

Code:
Date Month  Year  Time Event   Description             Remarks Duration        Closed
   9   JUN  2013  1932 2       Reset                   YYYYY
  10   JAN  2014  1000 4       Unspecified             YYYYY        50                 Y
  11   APR  2013  1230 2       Reset                   YYYYY
  12   FEB  2014  1142 4       Unspecified             YYYYY
  13   JAN  2015  0132 3       Reboot                  ZZZZZ                                   N
  11   FEB  2012  1032 3       Reboot                  ZZZZZ

But now the Problem with the Logic is that the following Line in the Code doesn't hold true for certain Lines
Code:
 if ($NF ~ /^(Y|N)$/)  printf "%s", $0}

(my fault, i should have given a better Raw-example). To highlight same, I am uploading a updated Raw file with 3 more lines.
I have tried to modify your code as follows:
Code:
awk     '/^Event/               {L=1}
         !L                     {next}
         /^Event/,/^END/        {REP[$1]=$0; next}
         /Durat/||NF==0         {next}
         /^END/ && L            {print ""; exit}
         /^Date/,/^END/         {if (NF > 2)    {printf "\n"
                                                 printf "%5s %5s %5s %5s %5s", $1,$2,$3,$4,REP[$5]}
                                 if ($1 == "Date") printf "\tDuration\tClosed\tStatus"
                                 if ($NF ~ /^Status$/)  getline;printf "%s", $0}
        ' test.txt test.txt

Based on keyword "Status", the Script should print next line.
This works fine, only issue is the Main line gets printed twice in the same line. Why , i am Confused Smilie
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

extract nth line of all files and print in output file on separate lines.

Hello UNIX experts, I have 124 text files in a directory. I want to extract the 45678th line of all the files sequentialy by file names. The extracted lines should be printed in the output file on seperate lines. e.g. The input Files are one.txt, two.txt, three.txt, four.txt The cat of four... (1 Reply)
Discussion started by: yogeshkumkar
1 Replies

2. Shell Programming and Scripting

awk/sed script to print each line to a separate named file

I have a large 3479 line .csv file, the content of which looks likes this: 1;0;177;170;Guadeloupe;x 2;127;171;179;Antigua and Barbuda;x 3;170;144;2;Umpqua;x 4;170;126;162;Coos Bay;x ... 1205;46;2;244;Unmak Island;x 1206;47;2;248;Yunaska Island;x 1207;0;2;240;north sea;x... (5 Replies)
Discussion started by: kalelovil
5 Replies

3. UNIX for Dummies Questions & Answers

How to separate a single column file into files of the same size (i.e. number of rows)?

I have a text file with 1,000,000 rows (It is a single column text file of numbers). I would like to separate the text file into 100 files of equal size (i.e. number of rows). The first file will contain the first 10,000 rows, the second row will contain the second 10,000 rows (rows 10,001-20,000)... (2 Replies)
Discussion started by: evelibertine
2 Replies

4. Shell Programming and Scripting

[Solved] making each word of a line to a separate line

Hi, I have a line which has n number of words with separated by space. I wanted to make each word as a separate line. for example, i have a file that has line like i am a good boy i want the output like, i am a good (8 Replies)
Discussion started by: rbalaj16
8 Replies

5. UNIX for Advanced & Expert Users

Parse key-value pair into separate rows

Hi, I'm getting key-value pairs in a string as follows - 0000xm7zcNDIkP888vRqGv93xA7:176n00qql||9700005405552747,9700005405717924,9700005405733788|unidentified,unidentified,unidentified I need output as follows - row1:... (5 Replies)
Discussion started by: sumoka
5 Replies

6. Shell Programming and Scripting

[Solved] How to separate one line to mutiple line based on certain number of characters?

hi Gurus, I need separate a file which is one huge line to multiple lines based on certain number of charactors. for example: abcdefghi high abaddffdd I want to separate the line to multiple lines for every 4 charactors. the result should be abcd efgh i hi gh a badd ffdd Thanks in... (5 Replies)
Discussion started by: ken6503
5 Replies

7. Programming

Read text from file and print each character in separate line

performing this code to read from file and print each character in separate line works well with ASCII encoded text void preprocess_file (FILE *fp) { int cc; for (;;) { cc = getc (fp); if (cc == EOF) break; printf ("%c\n", cc); } } int main(int... (1 Reply)
Discussion started by: khaled79
1 Replies

8. Shell Programming and Scripting

How to separate rows of data into another column?

I have data such as below where the value in second field is the same as that in the row after. 123456,22222,John,0,xyz 234567,22222,John1,1,cde 43212,3333,Jean,3,pip 84324,3333,Abel,2,cat I'd like to rearrange the output like below to put such records beside each other and separated with... (5 Replies)
Discussion started by: james2009
5 Replies

9. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

10. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies
All times are GMT -4. The time now is 11:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy