Line up Print outs from Separate Rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Line up Print outs from Separate Rows
# 8  
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
# 9  
Old 06-09-2014
Quote:
Originally Posted by Don Cragun
The problem with this is that you want the output columns aligned, but the data in your sample input tables is not aligned:

1. I can't align the output if I can't determine what data is in what field in the input. As long as there aren't any empty fields and all fields are separated by one or more tabs (like they are in the 1st table), we can easily align output data. But the 2nd table uses spaces between the 1st two fields and one, two, or three tabs between the 2nd and 3rd fields. If you have ten fields in your 2nd table, it has some empty fields, and uses a combination of spaces and tabs as field separators (or has data in any field that contains characters that are also used as field separators); we can't align the output. (Note that this also applies to the extended fields that appear on some lines. In the sample 1st table, the extended fields have two tabs at the start of the line for each extended header and extended data lines and have one, two, or three tabs separating fields. If these extended lines do not start with whitespace characters, have any empty fields, or use spaces as data with one or more fields and use spaces as field separators; we can't align the output.)

2.I had a good start on an awk script to gather data, calculate maximum field widths, and print aligned output, but it is useless under these conditions. I'm afraid that I don't see any way to produce code that will align output fields when the input can't be reliably separated into identifiable fields.

3.P.S. It would have also been nice if you would have warned us that your sample input file (test1.txt) is not a text file. The last four characters in the file are 'E', 'N', 'D', and a <tab> character; there is no <newline> character at the end of the partial line at the end of the file. I was trying to kick off certain actions when I found the END line at the ends of your tables, but some versions of awk completely ignore incomplete lines at the end of a file.

1. Some of the Input fields are out of Column while Copy pasting them in Notepad, Plz ignore them. Data is in Column format.
2. Thanks for the effort & will love to see u sharing them here. May be it will give some better Ideas to achieve my GoalSmilie
3. I have no idea about theseSmilie, else I would have surely highlighted in the description
Code:
sample input file (test1.txt) is not a text file.  The last four  characters in the file are 'E', 'N', 'D', and a <tab> character;

Smilie
# 10  
Old 06-09-2014
Instead of using Notepad, can you just cat file.txt to your screen and copy and paste from there? Or, can you just upload the file itself instead of getting Notepad invoked?

At least describe what the actual data format is in file.txt and file1.txt (since it is being obscured by Notepad):
  1. Are there tabs between fields, spaces between fields, or a combination of tabs and spaces between fields? If there are tab characters in your input files, are tab stops set at the standard 1, 9, (x*8)+1 column positions? If not, how are tab stops set? Do all fields start on columns where a tab stop is set?
  2. Is data in fields left justified, centered, or right justified under the headings?
  3. Do any of your data fields contain spaces or tabs that are not field separators?
  4. Do you want a line of output at the start or end of the output to explicitly say how many fields are in the output and how wide each field is?
  5. Do you need output in the last non-empty field to be space or tab padded (if output is not right justified)? Do empty fields at the end of an output line need to be space or tab filled? (In other words, is a constant width required for all output lines?)
  6. What is at the start of lines that contain extra data in the 1st table? Is it a specific number of spaces or tabs? By extra data, I mean lines like:
Code:
        Duration    Closed
        50            Y

# 11  
Old 06-09-2014
After your $NF ~ /^Status$/ test, you needed to enclose the statements in braces so both will executed on TRUE condition.
And, you stopped halfway ... try this, and compare:
Code:
awk     '/^Event/               {L=1}
         !L                     {next}
         /^Event/,/^END/        {REP[$1]=$0; next}
         NF==0                  {next}
         /^END/ && L            {print ""; exit}
         /^Date/,/^END/         {if ($NF ~ /^Status$/)  {getline;printf "%s", $0; next}
                                 if (NF > 2)    {printf "\n"
                                                 printf "%5s %5s %5s %5s %5s", $1,$2,$3,$4,REP[$5]}
                                 if ($1 == "Date") printf "\tDuration\tClosed\tStatus" }
        ' /tmp/test.txt /tmp/test.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question