Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-25-2012
pandeesh's Avatar
Registered User
 
Join Date: Jul 2011
Posts: 311
Thanks: 75
Thanked 12 Times in 12 Posts
Replacing 2 lines by single line

Hi
I have a file with below content :


Code:
a
b
S

I need to replace the lines which have a and b continuously by d.

Code:
d
S

I have used the below code

Code:
tr '\n' '#'<file|sed. 's/a#b/d/g's?|tr '#' '\n'

where # is not occurring anywhere in the file..
Is there any other efficient way to do this?

Thanks
Sponsored Links
    #2  
Old 06-25-2012
bartus11's Avatar
Registered User
 
Join Date: Apr 2009
Posts: 3,139
Thanks: 3
Thanked 954 Times in 933 Posts

Code:
perl -0pe 's/a\nb/d/g' file

The Following User Says Thank You to bartus11 For This Useful Post:
pandeesh (06-25-2012)
Sponsored Links
    #3  
Old 06-25-2012
pandeesh's Avatar
Registered User
 
Join Date: Jul 2011
Posts: 311
Thanks: 75
Thanked 12 Times in 12 Posts
what does -0 stand for?
    #4  
Old 06-25-2012
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Posts: 2,593
Thanks: 122
Thanked 716 Times in 599 Posts

Code:
sed '/a/{$q; N; s/a\nb/d/;}'

Regards,
Alister
Sponsored Links
    #5  
Old 06-25-2012
pandeesh's Avatar
Registered User
 
Join Date: Jul 2011
Posts: 311
Thanks: 75
Thanked 12 Times in 12 Posts
Hi Alister

Could you please explain how it works
Sponsored Links
    #6  
Old 06-26-2012
bartus11's Avatar
Registered User
 
Join Date: Apr 2009
Posts: 3,139
Thanks: 3
Thanked 954 Times in 933 Posts
From http://perldoc.perl.org/perlrun.html:
Code:
-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. Any value 0400 or above will cause Perl to slurp files whole, 
but by convention the value 0777 is the one normally used for this purpose.
You can also specify the separator character using 
hexadecimal notation: -0xHHH..., where the H are valid hexadecimal digits. Unlike the octal form, this one may be used to specify 
any Unicode character, even those beyond 0xFF. So if you really want a record separator of 0777, specify it as -0x1FF. 
(This means that you cannot use the -x option with a directory name that consists of hexadecimal digits, or else Perl will think you have 
specified a hex number to -0.)

Sponsored Links
    #7  
Old 06-26-2012
pandeesh's Avatar
Registered User
 
Join Date: Jul 2011
Posts: 311
Thanks: 75
Thanked 12 Times in 12 Posts
i am curious how to make sed to interpret "\n"?
the below is not working as expected:


Code:
sed 's/a\nb/d/g' file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replacing multi lines with 1 line Griffs_Revenge Shell Programming and Scripting 3 01-23-2012 07:54 PM
Multiple lines in a single column to be merged as a single line for a record Bhuvaneswari Shell Programming and Scripting 1 08-11-2011 03:16 AM
Break lines up into single lines after each space in every line lewk Shell Programming and Scripting 7 10-14-2009 09:33 AM
replacing multiple lines with single line siba.s.nayak Shell Programming and Scripting 3 05-28-2008 02:43 AM
replacing first line or lines in a file Terrible UNIX for Advanced & Expert Users 3 06-28-2006 08:23 PM



All times are GMT -4. The time now is 09:30 PM.