How to use sed to modify a line above or below matching pattern?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use sed to modify a line above or below matching pattern?
# 1  
Old 07-15-2011
Data How to use sed to modify a line above or below matching pattern?

I couldn't figure out how to use sed or any other shell to do the following. Can anyone help? Thanks.

If seeing a string (e.g., TODAY) in the line,

replace a string in the line above (e.g, replace "Raining" with "Sunny")
and replace a string in the line below (e.g., replace "Reading" with "Hiking")

Maggie
# 2  
Old 07-15-2011
Try:
Code:
perl -0pe 's/Raining(.*\n.*TODAY)/Sunny\1/g;s/(TODAY.*\n.*)Reading/\1Hiking/g' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-15-2011
sed is the stream oriented version of ed while ex is just ed extended...wonder why they didnt come up with a stream oriented version of ex Smilie
Code:
ex -s +'/TODAY/-1 s/Raining/Sunny/ | /TODAY/+1 s/Reading/Hiking/ | x' file


Last edited by shamrock; 07-15-2011 at 06:54 PM..
# 4  
Old 07-15-2011
Thanks a lot! They both works.
# 5  
Old 07-18-2011
can you explain what is "-0pe" and what "\1" mean in the command. thanks!

---------- Post updated 07-18-11 at 09:58 AM ---------- Previous update was 07-17-11 at 11:37 PM ----------

I need to go with Perl. but not familiar with this -0pe I need to replace the string in the file directly, can you help on how to modify the options to achieve that? Thanks a ton.
# 6  
Old 07-18-2011
Code:
man perlrun

Code:
-p   causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed:
    LINE:
       while (<>) {
           ...             # your program goes here
        } continue {
        print or die "-p destination: $!\n";
        }

   If a file named by an argument cannot be opened for some reason, Perl warns you about it, and moves on to the next file.  
   Note that the lines are printed automatically.  An error occurring during printing is treated as fatal.  
   To suppress printing use the -n switch.  A -p overrides a -n switch.

   "BEGIN" and "END" blocks may be used to capture control before or after the implicit loop, just as in awk.
....
-0[octal/hexadecimal]
   specifies the input record separator ($/) as an octal or hexadecimal number.  
   If there are no digits, the null character is the separator.  
   Other switches may precede or follow the digits.  For example, if you have a version of find which 
   can print filenames terminated by the null character, you can say this:

                find . -name ’*.orig’ -print0 │ perl -n0e unlink

   The special value 00 will cause Perl to slurp files in paragraph mode.  
   The value 0777 will cause Perl to slurp files whole because there is no legal byte with that value.

   If you want to specify any Unicode character, use the hexadecimal format: "-0xHHH...", 
   where the "H" are valid hexadecimal digits.  (This means that you cannot use
   the "-x" with a directory name that consists of hexadecimal digits.)

....
-e commandline
    may be used to enter one line of program.  If -e is given, Perl will not look for a filename in the argument list.  
    Multiple -e commands may be given to build up a multi-line script.  
   Make sure to use semicolons where you would in a normal program.

This User Gave Thanks to Skrynesaver For This Post:
# 7  
Old 07-18-2011
As for the "\1", it stores the first match contained in "()" brackets. To modify your file directly use this:
Code:
perl -i -0pe 's/Raining(.*\n.*TODAY)/Sunny\1/g;s/(TODAY.*\n.*)Reading/\1Hiking/g' file

This User Gave Thanks to bartus11 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

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

4. UNIX for Dummies Questions & Answers

Sed: Adding new line after matching pattern

Hi I just wanted to add a new line after every matching pattern: The method doing this doesn't matter, however, I have been using sed and this is what I tried doing, knowing that I am a bit off: sed 'Wf a\'/n'/g' Basically, I want to add a new line after occurrence of Wf. After the line Wf... (5 Replies)
Discussion started by: MIA651
5 Replies

5. UNIX for Dummies Questions & Answers

sed - combination of line deletion and pattern matching

I want to delete all the blank lines from a file before a certain line number. e.g. Input file (n: denotes line number) 1: a 2: 3: b 4: c 5: 6: d I want to delete all blank lines before line number 3, such that my output is: a b c d I see that sed '/^$/d' in_file works... (9 Replies)
Discussion started by: jawsnnn
9 Replies

6. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

7. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

8. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

10. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies
Login or Register to Ask a Question