awk removing sections of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk removing sections of a file
# 1  
Old 08-05-2010
awk removing sections of a file

I have a file that looks liek this (see below). can somebody provide me with and awk or sed command that can take a piece of the file starting from the time to the blank line and put in into another file.

For example: How would I get the data from 10:56:11 to the blank line.

Two things:

1) I want to leave the original file in tact.

2) I will know the value but my seconds may be off, so I would really only
want to match HH:MM instead of HH:MM:SS.

10:56:10
PID ELAPSED
1 11-18:33:15
73828 11-18:32:28
77906 11-18:32:29

10:56:11
PID ELAPSED
1 11-18:33:16
73828 11-18:32:29
77906 11-18:32:30
90208 11-18:32:30

10:58:10
PID ELAPSED
1 11-18:33:15
73828 11-18:32:28
77906 11-18:32:29

Thanks in advance to all who answer this post
# 2  
Old 08-05-2010
Code:
pattern="10:32"
awk -v pattern="$pattern"   '{if(index($0, pattern)==1) {ok=1}
                                        if(ok) {print $0}
                                        if(length($0)==0} {ok=0} ' inputfile > outputfile

# 3  
Old 08-05-2010
Quote:
Originally Posted by jim mcnamara
Code:
pattern="10:32"
awk -v pattern="$pattern"   '{if(index($0, pattern)==1) {ok=1}
                                        if(ok) {print $0}
                                        if(length($0)==0} {ok=0} ' inputfile > outputfile

I do appreicate the code snippet but there appears to be a syntax error in
it. Not being that familiar with awk can re-post it without the syntax issue. It would be greatly appreciated.
# 4  
Old 08-05-2010
Another approach, but without the seconds in the pattern you could get more pieces of the file:
Code:
awk -v time="10:56" 'index($0, time)==1{f=1}
f && !NF{f=0;print ""}
f' file

# 5  
Old 08-05-2010
Got it

pattern="10:32"
awk -v pattern="$pattern" '{ if (index($0, pattern)==1) {ok=1}
if(ok) {print $0}
if (length($0)==0) {ok=0} }' $inputfile > $outputfile
Thanks to all
# 6  
Old 08-05-2010
SED alternative:
Code:
 sed -e '/'"$pattern"'/!d; :pr' -e '/^$/b' -e ' n; b pr' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing sections from listener.ora

Hi, I am trying to write a script or command to add/remove a section from listener.ora file in the following example I would like: 1. to remove sid_alias2 sections 2. to add a new alias sections called sid_alias4, it can be placed after sid_alias3 sections $ cat listener.ora ... (18 Replies)
Discussion started by: ynixon
18 Replies

2. Shell Programming and Scripting

Omitting sections of file that contain word

I have a configuration file that contains hundreds of these chunks. Each "chunk" is the section that begins with "define service {" and ends with "}". define service { check_command check_proc!java hostgroup_name service_description ... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Programming

extract different sections of a file

Hi All, I have a file with the data 10;20;30;40;50;60;70;80;123;145;156;345. the output i want is the first fourth sixth elements and everything from there on. How do i achieve this. (1 Reply)
Discussion started by: raghu_shekar
1 Replies

4. Shell Programming and Scripting

Removing sections

I have a file like this %( PHASES P %) %( SOURCES (10,0.0) (13,0.0) (16,0.0) (19,0.0) (22,0.0) (25,0.0) (28,0.0) (31,0.0) (34,0.0) (37,0.0) (40,0.0) (1 Reply)
Discussion started by: kristinu
1 Replies

5. Shell Programming and Scripting

Removing sections and leaving separators intact

I have an awk script like below function abs(val) { return val > 0 ? val : -val } # 1. Main input loop, executed for each line of input BEGIN { RS = ORS = ">" } { if ( NF > 2 ) { if ( abs( $1 - $(NF-2) ) < 40 ) { print } } } The input file is something like... (2 Replies)
Discussion started by: kristinu
2 Replies

6. Shell Programming and Scripting

Modify sections of the line in a file

Hello.. I have a line in a file which I have to edit: the line looks like: <!]> Sometimes, the section of the line can have only one entry for cn, or maybe more than 2 like below: <!]> I have a variable which has the following value: CN="(cn=MNO)(cn=XYZ)" I need to replace the part... (4 Replies)
Discussion started by: chiru_h
4 Replies

7. Shell Programming and Scripting

Parsing file, yaml file? Extracting specific sections

Here is a data file, which I believe is in YAML. I am trying to retrieve just the 'addon_domains" section, which doesnt seem to be as easy as I had originally thought. Any help on this would be greatly appreciated!! I have been trying to do this in awk and mostly bash scripting instead of perl... (3 Replies)
Discussion started by: Rhije
3 Replies

8. Shell Programming and Scripting

Best way to remove sections of text from a file

Greetings! I found this fourm via a google search on "sed expressions". I have a file that contains notices and they are all the same length in lines. For example the file would contains 15 notices, each being 26 lines each. I need some way to eliminate notices that contain a "S" in a particular... (8 Replies)
Discussion started by: cals64
8 Replies

9. Shell Programming and Scripting

extract multiple sections of file

I have a file that I need to parse multiple sections from the file. The file contains multiple lines that start with ST (Abunch of data) Then the file contains multiple lines that start with SE (Abunch of data) SE*30*0001 ST*810*0002 I need all of the lines between and including these.... (6 Replies)
Discussion started by: rgentis
6 Replies

10. UNIX for Advanced & Expert Users

extract multiple sections of a file

I have a file that I need to parse multiple sections from the file. The file contains multiple lines that start with ST (Abunch of data) Then the file contains multiple lines that start with SE (Abunch of data) SE*30*0001 ... (1 Reply)
Discussion started by: rgentis
1 Replies
Login or Register to Ask a Question