[awk] find pattern, change next two lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [awk] find pattern, change next two lines
# 1  
Old 04-15-2014
[awk] find pattern, change next two lines

Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills":

I need to find a pattern /VEL/ in an input file that looks like this:

Code:
 1110SOL     OW25489   1.907   7.816  26.338 -0.4365  0.4100 -0.0736
 1110SOL    HW125490   1.890   7.829  26.435  0.3507  0.4216  0.0705
 1110SOL    HW225491   1.945   7.724  26.323  0.0656  0.6460 -0.2797
 1111VEL     OW25492   4.172   7.242   6.227  0.4897 -1.0759  0.7407
 1111SOL    HW125493   4.231   7.300   6.284 -1.1002 -0.8260  1.3902
 1111SOL    HW225494   4.076   7.266   6.243 -0.8879 -1.4622  1.1007
 1112SOL     OW25495   0.578   1.347  21.833 -0.3603 -0.4613 -0.2495
 1112SOL    HW125496   0.550   1.375  21.741 -0.2047  1.9848  0.3727
 1112SOL    HW225497   0.506   1.291  21.874 -0.7886 -0.8859 -1.5335

...and then replace the SOL in the following two lines with VEL, so it looks like this:

Code:
 1110SOL     OW25489   1.907   7.816  26.338 -0.4365  0.4100 -0.0736
 1110SOL    HW125490   1.890   7.829  26.435  0.3507  0.4216  0.0705
 1110SOL    HW225491   1.945   7.724  26.323  0.0656  0.6460 -0.2797
 1111VEL     OW25492   4.172   7.242   6.227  0.4897 -1.0759  0.7407
 1111VEL    HW125493   4.231   7.300   6.284 -1.1002 -0.8260  1.3902
 1111VEL    HW225494   4.076   7.266   6.243 -0.8879 -1.4622  1.1007
 1112SOL     OW25495   0.578   1.347  21.833 -0.3603 -0.4613 -0.2495
 1112SOL    HW125496   0.550   1.375  21.741 -0.2047  1.9848  0.3727
 1112SOL    HW225497   0.506   1.291  21.874 -0.7886 -0.8859 -1.5335

Thanks for any help.
# 2  
Old 04-15-2014
Code:
awk '/VEL/{c=3}c-->=1{sub("SOL","VEL")}1' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-15-2014
Thanks a lot, Yoda, can you explain the syntax to me? I don't want to die stupid.
# 4  
Old 04-15-2014
Quote:
Originally Posted by origamisven
Thanks a lot, Yoda, can you explain the syntax to me? I don't want to die stupid.
Sure.
Code:
awk '
        # Look for pattern VEL in each record
        /VEL/ {
                # if found set variable c value to 3
                c = 3
        }
        # For each subsequent records decrement variable c value by 1 and check if it is >= 1
        # This condition is to replace next 2 records as you requested
        c-- >= 1 {
                # Substitute SOL with VEL
                sub ( "SOL", "VEL")
        }
        # 1 == true, default awk operation is print the record
        1
' file

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to remove pattern and lines above pattern

In the awk below I am trying to remove all lines above and including the pattern Test or Test2. Each block is seperated by a newline and Test2 also appears in the lines to keep but it will always have additional text after it. The Test to remove will not. The awk executed until the || was added... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Find specific pattern and change some of block values using awk

Hi, Could you please help me finding a way to replace a specific value in a text block when matching a key pattern ? I got the keys and the values from a command similar to: echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1... (2 Replies)
Discussion started by: alex2005
2 Replies

3. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

4. Shell Programming and Scripting

Find pattern; grep n lines before and after

Hi, I need help to grep a specific part of a log file (bold). 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: } 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: extensionContainer <Not Present> 24/2/2017-16:57:17.056... (8 Replies)
Discussion started by: vasil
8 Replies

5. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

6. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

7. Shell Programming and Scripting

how to find pattern and discard lines before it?

Hi all, I'd like to search a file for the first occurence of the phrase "PLASTICS THAT EXPIRE" and then discard all the lines that came before it. Output the remainder to a new file. Operating system is hp-ux. I've searched for usual awk and sed one liners but can't find a solution. Thank... (4 Replies)
Discussion started by: Scottie1954
4 Replies

8. Shell Programming and Scripting

Find and change the lines in the file.

Hi Everyone, I have a JIL (Job Information Language) Definition for Several Jobs in a file. I would like to do the following: insert_job: KS_INT_BIZ_INT job_type: c command: /home/filter/script.sh INT machine: MACHINE owner: filter permission: gx,mx date_conditions: 1 days_of_week:... (3 Replies)
Discussion started by: filter
3 Replies

9. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

10. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies
Login or Register to Ask a Question