Visit Our UNIX and Linux User Community


Use string as Record separator in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use string as Record separator in awk
# 1  
Old 06-19-2015
Use string as Record separator in awk

Hello to all,

Please some help on this. I have the file in format as below.

How can I set the record separator as the string below in red
"No. Time Source Destination Protocol Length Info"

I've tried code below but it doesn't seem to work.
Code:
awk 'BEGIN{RS = "No.     Time.*"} ; {print $0}' file

input file:
Code:
No.     Time                          Source                Destination           Protocol Length Info
     28 2015-06-18 12:12:05.727772000 001822                  780568                  XXXXX  198    XXXXX

Frame 28: 198 bytes on wire (1584 bits), 198 bytes captured (1584 bits) on interface 0
    Interface id: 0 (\Device\NPF_{28086D60-7061-4ADD-B9})
    Encapsulation type: Ethernet (1)
	
	
No.     Time                          Source                Destination           Protocol Length Info
     29 2015-06-18 12:12:05.757809000 1.1.1.14          1.1.1.2          MXHHH     62     ZZZZ 

Frame 29: 62 bytes on wire (496 bits), 62 bytes captured (496 bits) on interface 0

No.     Time                          Source                Destination           Protocol Length Info
     30 2015-06-18 12:12:05.759481000 1.1.1.1          2.2.2.20           MHCKK     62     ZZZZ 

Frame 30: 62 bytes on wire (496 bits), 62 bytes captured (496 bits) on interface 0
    Interface id: 0 (\Device\NPF_{28086D60-7061-4ADD-B9A1})

Thanks for any help
# 2  
Old 06-19-2015
It depends on your version of awk. Regular awk can only use a single character as RS . GNU awk and mawk can use a regular expression..

For example:
Code:
$ gawk 'BEGIN{ORS=""; RS="No.     Time[^\n]*\n"} NR==2'  file
     28 2015-06-18 12:12:05.727772000 001822                  780568                  XXXXX  198    XXXXX

Frame 28: 198 bytes on wire (1584 bits), 198 bytes captured (1584 bits) on interface 0
    Interface id: 0 (\Device\NPF_{28086D60-7061-4ADD-B9})
    Encapsulation type: Ethernet (1)


--

( I inserted the -- manually otherwise the empty lines will not print, they are not there ins the actual output )




--
Alternative way of writing it:
Code:
gawk 'NR==2' RS="No.     Time[^\n]*\n" ORS= file


Last edited by Scrutinizer; 06-19-2015 at 06:17 PM..
# 3  
Old 06-19-2015
Hello Scrutinizer,

Thanks for your reply.

I've tried your solution but I only receive the first record and if I try to print the first field for all records (28, 29 and 30), I only receive the first one (28). The same if I try to print the 2nd field(the dates) I onle receive the first date.

like this:
Code:
gawk 'BEGIN{ORS=""; RS="No.     Time[^\n]*\n"} NR==2 {print $1}' file
28

The output should be:
Code:
28
29
30

Thanks again for the help
# 4  
Old 06-19-2015
Code:
gawk 'BEGIN{ORS=""; RS="No.     Time[^\n]*\n"} NR==2 {print $1}' file

That will fail to display what you want for the two parts in red. And because record one needs to be excluded or it will display a blank line

If all you want is:
Code:
28
29
30

Try:
Code:
awk '$1 ~ /^[0-9][0-9]$/ {print $1}' file

# 5  
Old 06-19-2015
Hello Aia,

Thanks for the help, but actually I want to set that string as Record Separator since each block begins with "No. ...." and I need to process further.

Thanks for any help
# 6  
Old 06-20-2015
To get all the first fields, try:
Code:
gawk 'NR>1{print $1}' RS="No.     Time[^\n]*\n"  file

# 7  
Old 06-20-2015
Hi Scrutinizer,

Many thanks.

Your last code it seems to work. I'll try using this was in my further analsys.

Regards

Previous Thread | Next Thread
Test Your Knowledge in Computers #285
Difficulty: Easy
AIX was the first operating system to have a proc file system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with awk regular expression for RS record separator

Hi, I'm using gawk to read a text file and count the sentences. I want to use a record separator of a period, exclamation mark and a question mark. The problem is that the file contains words like "Mr. Smith" so the periods in the appellation are tripping my record separator. This is my... (12 Replies)
Discussion started by: 1Brajesh
12 Replies

2. Shell Programming and Scripting

Replace a string for every record after the 1st record

I have data coming in the below format for each record <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox> <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox... (8 Replies)
Discussion started by: dsravanam
8 Replies

3. Shell Programming and Scripting

awk - single quotes as record separator

How do I use single quotes as record separator in awk? I just couldn't figure that out. I know how to use single quotes as field separator, and double quotes as both field and record separator ... (1 Reply)
Discussion started by: locoroco
1 Replies

4. Shell Programming and Scripting

apply record separator to multiple files within a directory using awk

Hi, I have a bunch of records within a directory where each one has this form: (example file1) 1 2 50 90 80 90 43512 98 0909 79869 -9 7878 33222 8787 9090 89898 7878 8989 7878 6767 89 89 78676 9898 000 7878 5656 5454 5454 and i want for all of these files to be... (3 Replies)
Discussion started by: amarn
3 Replies

5. Shell Programming and Scripting

awk, string as record separator, transposing rows into columns

I'm working on a different stage of a project that someone helped me address elsewhere in these threads. The .docs I'm cycling through look roughly like this: 1 of 26 DOCUMENTS Copyright 2010 The Age Company Limited All Rights Reserved The Age (Melbourne, Australia) November 27, 2010... (9 Replies)
Discussion started by: spindoctor
9 Replies

6. Shell Programming and Scripting

Using > as record separator

I have tried to use ">" as record separator, but it doesn't work. I have tried this: awk BEGIN{RS=">"}'{print $0}' input output: awk: BEGIN{RS=>}{print $0} awk: ^ syntax error awk BEGIN{RS="\>"}'{print $0}' input awk: BEGIN{RS=\>}{print $0} awk: ^ backslash not... (2 Replies)
Discussion started by: locoroco
2 Replies

7. Shell Programming and Scripting

awk - double quotes as record separator

How do I use double quotes as a record seperator in awk? (4 Replies)
Discussion started by: locoroco
4 Replies

8. Shell Programming and Scripting

How to add record separator after certain lines

How to add record separator after certain lines? I am faicing issue where some lines have result as successive line & some are not having. how can I add record separator after every record here is example of data I have (line numbers are not present in data): 1. <enabled="true" name="dSuite1"... (7 Replies)
Discussion started by: sach253
7 Replies

9. Shell Programming and Scripting

awk & cut record separator problem

Hi All, I've got some strange behaviour going on when trying to manipulate a file that contains spaces. My input file looks something like this: xxxxxxxxx,yyyy,sss sss sss,bbbbbbb If I use awk: When running from the command line I get: sss sss sss But when running from a... (7 Replies)
Discussion started by: pondlife
7 Replies

10. Shell Programming and Scripting

record separator

can anyone tell me any way to change record separator (default is new line). RS in nawk as not working. Thanks in advance. Regards Rochit (7 Replies)
Discussion started by: rochitsharma
7 Replies

Featured Tech Videos