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
# 1  
Old 06-06-2014
Line up Print outs from Separate Rows

A text file has two logs of same event & both logs have to be correlated into a single line before same can be processed further

RAW
Code:
RunningLog

Date    Month    Year    Time    Event
9        JUN        2013    1932    2
10        JAN        2014    1000    4
        Duration    Closed
        50            Y
        
11        APR        2013    1230    2
12        FEB        2014    1142    4
13        JAN        2015    0132    3
        Duration    Closed
                          N

11        FEB        2012    1032    3
END

RunningLog
Event Description    Remarks
1     test            XXXXX
2     Reset            YYYYY
3     Reboot        ZZZZZ
4     Unspecified    YYYYY
END

The final column in the first table needs to be looked up in the 2nd table & the matching entry has to be copied to the first table & appended to the end of the Line. Both Tables in the same file. Some entries have extra information, which needs to (Should) be lined up at the end of Line as its length is inconsistent.

Output
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        40            N
11        FEB        2012    1032    3     Reboot        ZZZZZ

Attached a raw file for Simulation.
I have tried with awk & partially successful; but now I am trying for Shell.

Plz suggest some way to approach the same ....
# 2  
Old 06-06-2014
Show us what you did with awk that was partially successful.
Show us what you have tried with shell.
# 3  
Old 06-07-2014
Code:
awk 'begin{FS="\t";} /^Event1/,/^END/{i=$1;a[i]=$0} /^Date/,/^END/{if(NR>FNR && NF>2) {printf "%s\t %s\t %s\t %s\t %s\n" ,$1,$2,$3,$4,a[$5]}}' test.txt test.txt

Output
Code:
Date     Month   Year    Time
9        JUN     2013    1932    2      Reset                   YYYYY
10       JAN     2014    1000    4      Unspecified             YYYYY
11       APR     2013    1230    2      Reset                   YYYYY
12       FEB     2014    1142    4      Unspecified             YYYYY
13       JAN     2015    0132    3      Reboot                  ZZZZZ
11       FEB     2012    1032    3      Reboot                  ZZZZZ

# 4  
Old 06-07-2014
As long as the output lines up in columns, do you care whether the output field separators are spaces, tabs, or combinations of spaces and tabs? (Note that your input file has one or two tabs as field separators, and your awk program is using a tab followed by a space as an output field separator.)

Will input data in any field ever contain one or more spaces? If there are spaces in any input field, does that preclude ever using space in an output field separator?

Will the 2nd RunningLog table always contain 3 fields? Or, does you program have to be able to adjust for any number of fields in the 2nd input table?
# 5  
Old 06-08-2014
This is definitely not a masterpiece of coding, but it would do what is requested on the very sample file given:
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

# 6  
Old 06-08-2014
Quote:
Originally Posted by Don Cragun
As long as the output lines up in columns, do you care whether the output field separators are spaces, tabs, or combinations of spaces and tabs? (Note that your input file has one or two tabs as field separators, and your awk program is using a tab followed by a space as an output field separator.)

Will input data in any field ever contain one or more spaces? If there are spaces in any input field, does that preclude ever using space in an output field separator?

Will the 2nd RunningLog table always contain 3 fields? Or, does you program have to be able to adjust for any number of fields in the 2nd input table?
1. Tabs or Spaces doesn't matter, ultimately the output has to go to excel & excel will be able to convert it to Columns on both Tabs/Spaces

2. 2nd Table has around 10 Fields but some fields may or may not carry data, thats why I have tried to copy the complete the line ....
# 7  
Old 06-08-2014
Quote:
Originally Posted by newageBATMAN
1. Tabs or Spaces doesn't matter, ultimately the output has to go to excel & excel will be able to convert it to Columns on both Tabs/Spaces

2. 2nd Table has around 10 Fields but some fields may or may not carry data, thats why I have tried to copy the complete the line ....
The problem with this is that you want the output columns aligned, but the data in your sample input tables is not aligned:
Code:
RunningLog

Date	Month	Year	Time	Event
9		JUN		2013	1932	2
10		JAN		2014	1000	4
		Duration	Closed
		50			Y
		
11		APR		2013	1230	2
12		FEB		2014	1142	4
13		JAN		2015	0132	3
		Duration	Closed
		40			N

11		FEB		2012	1032	3
END

RunningLog
Event Description	Remarks
1     test			XXXXX
2     Reset			YYYYY
3     Reboot		ZZZZZ
4     Unspecified	YYYYY
END

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

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.

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