Replacing 2 lines by single line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing 2 lines by single line
# 1  
Old 06-25-2012
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
# 2  
Old 06-25-2012
Code:
perl -0pe 's/a\nb/d/g' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-25-2012
what does -0 stand for?
# 4  
Old 06-26-2012
Code:
sed '/a/{$q; N; s/a\nb/d/;}'

Regards,
Alister
# 5  
Old 06-26-2012
Hi Alister

Could you please explain how it works
# 6  
Old 06-26-2012
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.)

# 7  
Old 06-26-2012
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a single line with multiple lines in a file

Hi Am confused with the usage of "sed" command I want to replace a single line with multiple lines of a file.. eg., A file has Hi, How are you? I need to replace as Am fine What are You doing? I used the script as string1="Hi, How are you?" echo "$string1 is the value"... (4 Replies)
Discussion started by: Priya Amaresh
4 Replies

2. Shell Programming and Scripting

Write the lines in one single line

Hi, I need some iteration to do the following work. Sample: ANS|26-Jan-2012|26|MON|12536.1 ANS|26-Jan-2012|26|TUE|2536.1 ANS|26-Jan-2012|26|THUR|789.1 SED|26-Jan-2013|32|MON|258.1 SED|26-Jan-2013|32|TUE|369.1 SED|26-Jan-2013|32|THUR|2145.1 OUTPUT: ... (3 Replies)
Discussion started by: anshaa
3 Replies

3. Shell Programming and Scripting

replacing multi lines with 1 line

I have an xml file that is stripped down to output that looks bacically like; <!-- TABLEA header --> <tablea> some fields </tablea> <!-- TABLEB header --> <!-- TABLEC header --> <tablec> some fields </tablec> I want to remove the header... (3 Replies)
Discussion started by: Griffs_Revenge
3 Replies

4. Shell Programming and Scripting

Multi lines to single line

HI, My input file contains the data as like below: A1234119993 B6271113 Bghjkjk A1234119992 B6271113hi Bghjkjkmkl the output i require is : A1234119993 B6271113 Bghjkjk A1234119992 B6271113hi Bghjkjkmkl Please help me in this. Thanks (6 Replies)
Discussion started by: pandeesh
6 Replies

5. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

6. Shell Programming and Scripting

split single line into two line or three lines

Dear All, I want to split single line into two line or three lines wherever “|” separated values comes using Input line test,DEMTEMPUT20100404010012,,,,,,,,|0070086|0070087, output shoule be test,DEMTEMPUT20100404010012,,,,,,,,0070086, test,DEMTEMPUT20100404010012,,,,,,,,0070087, (14 Replies)
Discussion started by: arvindng
14 Replies

7. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

8. Shell Programming and Scripting

replacing multiple lines with single line

Can any one give me the idea on replacing multiple blank lines with a single blank line? Please conside it for a file having more than 100 number of characters. Regards, Siba (3 Replies)
Discussion started by: siba.s.nayak
3 Replies

9. UNIX for Advanced & Expert Users

replacing first line or lines in a file

hey guys, how do i replace only a line within a file without messing up the rest of the contents of the file? see, if possible can you guys give me a straight forward way to do this. i dont want a complex command. what i mean is i know i can accomplish this by using sed, well, i think i can,... (3 Replies)
Discussion started by: Terrible
3 Replies

10. UNIX for Dummies Questions & Answers

Joining lines to single line in VI

Dear friends, In VI, I have these data shown below: Line1 Line2 Line3 Line4 How can I JOIN these line to the first line? When I finished I should have: Line1 Line2 Line3 Line4 is there a text length limit of how long a single line can be in VI? Thank you much! (10 Replies)
Discussion started by: bobo
10 Replies
Login or Register to Ask a Question