Print first and last line from multiline record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print first and last line from multiline record
# 1  
Old 05-15-2012
Print first and last line from multiline record

Hi - I'm new to working with multiline records and I'm going nuts trying to do something that seems simple.

Input:
Code:
Tue May 1 14:00 Header Record 1 is valid.
Tue May 1 14:00 processing data to 25-Mar-2012 09:00:23.15
Tue May 1 14:03 Header Record 1 is valid.
Tue May 1 14:03 processing data to 25-Mar-2012 09:05:12.19
Tue May 1 14:04 processing data to 25-Mar-2012 09:06:23.67
Tue May 1 14:04 processing data to 25-Mar-2012 09:06:24.72
Tue May 1 14:05 Header Record 1 is valid.
Tue May 1 14:05 processing data to 25-Mar-2012 09:06:33.02
Tue May 1 14:08 processing data to 25-Mar-2012 09:12:34.24

What I'd like to see is this:
Code:
Tue May 1 14:00 Header Record 1 is valid. @ Tue May 1 14:00 processing data to 25-Mar-2012 09:00:23.15
Tue May 1 14:03 Header Record 1 is valid. @ Tue May 1 14:04 processing data to 25-Mar-2012 09:06:24.72
Tue May 1 14:05 Header Record 1 is valid. @ Tue May 1 14:08 processing data to 25-Mar-2012 09:12:34.24

Moderator's Comments:
Mod Comment Code tags please

i.e. each record starts with the header record and ends with the last processing data line, but only print the header record, some delimiter then the final field of the record.

I tried this:
Code:
BEGIN {
    FS="\n"
    RS="@"
}
{ print $1 "@" $NF }

but it only prints the very first line of the file, then all the final record lines.

Can someone tell me where I'm going wrong.

Thanks

Last edited by Scrutinizer; 05-15-2012 at 09:01 AM.. Reason: Please use code tags - again
# 2  
Old 05-15-2012
By using a records selector "@" that is not in your file, the whole file becomes one big record. By using "\n" as the field selector, the first line becomes $1 and the last line becomes $NF.
# 3  
Old 05-15-2012
Code:
awk '/Header/{printf "%s @ ", $0}/processing/{x=$0; getline; if($0~/Header/){printf "%s\n%s @ ", x, $0}else{x=$0}} END {printf "%s", x}' inputfile

# 4  
Old 05-15-2012
Alternatively:
Code:
awk '/Header/{if(p)print h,"@",p; h=$0; next}{p=$0} END{print h,"@",p}' infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-16-2012
The code from Balasejuri mostly worked, but omitted the very last "processing data" line.

Scrutinzer's code worked perfectly.

Thanks to both for your quick replies. I can stop tearing my hair out now!

---------- Post updated 16-05-12 at 10:45 AM ---------- Previous update was 15-05-12 at 01:57 PM ----------

Hi Scrutinizer - I've been trying to work out how your awk one-liner works, without success. Could you decode it for me.

Thanks

---------- Post updated at 11:46 AM ---------- Previous update was at 10:45 AM ----------

I didn't say, but sometimes the second pattern - 'processing data' - doesn't appear and there are two Header lines in a row. Scrutinizer's awk prints the last occurrence of the processing data line - can it just print the Header and move onto the next record?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine multiline to one line till a blank line

Hello, I have a file as :- ABC DEF GHI JKL <BlankLine> MNO PQR STU VWX <BlankLine> YZA I need it as below:- ABCDEFGHIJKL; MNOPQRSTUVWX; (3 Replies)
Discussion started by: jassi10781
3 Replies

2. Shell Programming and Scripting

Help with print out record if first and next line follow specific pattern

Input file: pattern1 100 250 US pattern2 50 3050 UK pattern3 100 250 US pattern1 70 1050 UK pattern1 170 450 Mal pattern2 40 750 UK . . Desired Output file: pattern1 100 250 US pattern2 50 3050 UK pattern1 170 450 Mal pattern2... (3 Replies)
Discussion started by: cpp_beginner
3 Replies

3. Shell Programming and Scripting

Help with print out line that have different record in specific column

Input file 1: - 7367 8198 - 8225 9383 + 9570 10353 Input file 2: - 2917 3667 - 3851 4250 + 4517 6302 + 6302 6740 + 6768 7524 + 7648 8170 + 8272 8896 + 8908 9915 - 10010 ... (18 Replies)
Discussion started by: perl_beginner
18 Replies

4. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

5. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

6. Shell Programming and Scripting

Regarding multiline record searching with specific pattern

Dear Experts, I need to extract specific records from one file which has multiline records. Input file pattern is: ============ aaaaaaaa bbbbbbbb asdf 1234 cccccccc dddddddd ============ aaaaaaaa bbbbbbbb qwer 2345 cccccccc dddddddd (7 Replies)
Discussion started by: dhiraj4mann
7 Replies

7. Shell Programming and Scripting

Multiline read with multicharacter record seperator

I have a file like the below: Start <</NumCopies 0001>> 0223 098 et(5926)sh 0223 098 mt(5926)sh End Start <</NumCopies 0001>> 0224 098 et(5926)sh 0224 098 mt(5926)sh End This file needs to be split to seperate files. Each of the seperate file will need to... (3 Replies)
Discussion started by: pt14
3 Replies

8. Shell Programming and Scripting

Awk Multiline Record Combine?

I'm trying to use Awk to get the id and name fields ($1 and $2) of file1 combined with their corresponding multiline records in file2 that are separated by blank line. Both files are ordered so that the first line of file1 corresponds to the first set of multiline records in file2 and so on. ... (4 Replies)
Discussion started by: RacerX
4 Replies

9. UNIX for Advanced & Expert Users

Print Full record and substring in that record

I have i got a requirement like below. I have input file which contains following fixed width records. 00000000000088500232007112007111 I need the full record and concatenated with ~ and characters from 1to 5 and concatenated with ~ and charactes from 10 to 15 The out put will be like... (1 Reply)
Discussion started by: ukatru
1 Replies

10. Shell Programming and Scripting

transforming a multiline record to single line

Hi All I have a file like this <LText>gvsvdkag<LREC>bdjvdj</LREC>nididyvv</LText> <LText>gvsvdkag<LREC>bdj vdj</LREC>nididyvv</LText> <LText>gvsvdkag<LREC>b djvdj</LREC>nididyvv</LText> <LText>gvsvdkag<LREC>bdjvdj</LREC>nididyvv</LText> How will i change the file to ... (9 Replies)
Discussion started by: anju
9 Replies
Login or Register to Ask a Question