Sponsored Content
Top Forums Shell Programming and Scripting How to use sed to modify a line above or below matching pattern? Post 302539681 by Skrynesaver on Monday 18th of July 2011 11:15:53 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
atmhosts(4)						     Kernel Interfaces Manual						       atmhosts(4)

NAME
atmhosts - ATM host name file DESCRIPTION
The atmhosts file is an ASCII file that contains an single-line entry for each host on the ATM network. Each entry consists of an ATM host address, official host name, and any aliases. Parameters are separated by spaces or tab characters. A number sign (#) indicates the beginning of a comment; characters up to the end of the line are not interpreted by routines that search the file. ATM host addresses are specified by an even number of hexadecimal digits. Each pair of digits represents 8 bits of address information. An ATM address can be one of the following: An ATM End System Address (AESA) as registered with the network (19 bytes, or 38 hexadecimal digits) A local Endpoint System Identifier (ESI) that is registered with the local switch (6 bytes, or 12 hexadecimal digits) An AESA with selector byte, representing the address of a service on a remote machine (20 bytes, or 40 hexadecimal digits). Note By default, the atmhosts file contains an entry for Permanent Virtual Circuits (PVCs). The entry consists of an ATM address of 40 hexadeci- mal zeros (0) and the host name PVC. Do not change or delete this entry. FILES
ATM hosts file RELATED INFORMATION
Commands: atmconfig(8), atmarp(8) delim off atmhosts(4)
All times are GMT -4. The time now is 06:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy