sed to replace two lines with one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to replace two lines with one
# 1  
Old 09-20-2010
sed to replace two lines with one

I want to use sed to check if a short line is contained in the line after it, and if it is, to delete the short one. In other words, the input is...

This is a
This is a line

... and I want it to give me...

This is a line

Here's what I've tried so far: s/\(^.*\)\n\(\1.*$\)/\2/

Also, is there a way to get sed to act directly on the input file, as opposed to a screen dump or creating an output file?
# 2  
Old 09-20-2010
Code:
sed -n 'n;p'

To edit files in place, take a look at the ed command. If for some reason that's not suitable, if you're using gnu sed, take a look at the -i option.

Regards,
Alister
# 3  
Old 09-20-2010
This is perfect. Thanks!
# 4  
Old 09-20-2010
Are you sure alister's code resolved your problem? Or you changed your idea?

Alister's code only use to print even lines.
# 5  
Old 09-20-2010
Ah.... I wondered how it cut so many lines. It worked for the cases I looked for, but I should have looked closer.

No, that won't do at all. I need to inspect each line, and compare it to the next.

Can you tell me why what I have isn't working for me? I'm trying to take the first line, see if the second line is the first with any more characters, and then if so, replace the whole thing with the second line.
# 6  
Old 09-20-2010
Woops. Obviously I missed the crux of the problem. Apologies for the noise. Smilie

---------- Post updated at 09:33 PM ---------- Previous update was at 09:26 PM ----------

How about:
Code:
sed 'N; /^\([^\n]\{1,\}\)\n\1/s/.*\n//'

That will inspect a pair of lines to see if the first is a leading substring of the next. Then it moves on to the next pair of lines. There is no overlap between pairs. 1-2, 3-4, 5-6, etc ... are inspected, not 1-2, 2-3, 3-4. Your problem statement wasn't explicit in this regard, so I chose the simpler of the two to implement.

Example:
Code:
$ cat data
ab
abcd
12
345
ef
efgh
$ sed 'N; /^\([^\n]\{1,\}\)\n\1/s/.*\n//' data
abcd
12
345
efgh

Regards,
Alister
# 7  
Old 09-20-2010
Not really correct:

Code:
cat infile

XXXX
ab
abcd


sed 'N; /^\([^\n]\{1,\}\)\n\1/s/.*\n//' infile
XXXX
ab
abcd

cat infile

ab
abcd

sed 'N; /^\([^\n]\{1,\}\)\n\1/s/.*\n//' infile
abcd



---------- Post updated at 11:52 AM ---------- Previous update was at 11:49 AM ----------

and if you see this situation, what's your expect output?

Code:
ab
abc
abcd
abcde

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Replace one line with two lines

Literally cannot get this one, guys. Single line replacement is simple, but I am not understanding the correct syntax for including a new line feed into the substitution part. Here's what I got. (Cannot use perl) #!/bin/sh set -f #Start Perms export HOME=/home/test_user # End Perms... (6 Replies)
Discussion started by: Parallax
6 Replies

2. Shell Programming and Scripting

sed to replace a line with multi lines from a var

I am trying to find a line in a file ("Replace_Flag") and replace it with a variable which hold a multi lined file. myVar=`cat myfile` sed -e 's/Replace_Flag/'$myVar'/' /pathto/test.file myfile: cat dog boy girl mouse house test.file: football hockey Replace_Flag baseball ... (4 Replies)
Discussion started by: bblondin
4 Replies

3. Shell Programming and Scripting

Replace multiple lines through sed

Hi All, I have a input file as sample below <this is not starting of file> record line1 line2 line3 end line4 line5 record line6 line7 line8 my requirement is this, i want to select a pattern between first record and end, whatever is written between first record and end. and... (0 Replies)
Discussion started by: adgangwar
0 Replies

4. Shell Programming and Scripting

How to replace a text containing new lines using sed or any other method?

Hi, i want to replace "Hi How are You when did you go to delhi" to "Hi How are you when did you come from delhi" in a file. Any idea how to do it? (2 Replies)
Discussion started by: abhitanshu
2 Replies

5. Shell Programming and Scripting

sed to replace a line with 2 or more different lines in same file

i have something like this... cat filename.txt <complexType name="abc"> bklah vlah blah blha blha blah blha </complexType > <complexType name="def"> bklah vlah blah blha blha blah blha </complexType > . . .and so on.. its a very large file (11 Replies)
Discussion started by: vivek d r
11 Replies

6. Shell Programming and Scripting

perl or sed replace lines only if for all files

Hi Everyone, # cat 3 a b # cat 4 a b # perl -p -i -e "s/.*/c/ if $.==2" * # cat 3 a c # cat 4 a b (4 Replies)
Discussion started by: jimmy_y
4 Replies

7. Shell Programming and Scripting

Replace multiple lines between tags using sed

I have a file example.txt with content look like this: <TAG> 1 2 3 </TAG> and I use a sed command to replace everything between <TAG></TAG> as below: sed -e 's/\(<TAG>\)*\(<.*\)/something/g' example.txt > example.txt.new But unfortunately, the command failed to replace as i want, it... (23 Replies)
Discussion started by: dollylamb
23 Replies

8. Shell Programming and Scripting

sed find and replace multiple lines

I am new to linux and would like to modify the contents of a file preferably using a one line. The situation is as follows <start> some lines "I am the string" "replace string" more lines here <end> In the above example,On encountering "I am the string", the "replace string "should be... (6 Replies)
Discussion started by: supersimha
6 Replies

9. Shell Programming and Scripting

SED - Search and replace lines containing...

Hi, I need a sed line that will find all lines that contain "<int key="NSWindowStyleMask">" and then replace the entire line (not just that one string) with "<int key="NSWindowStyleMask">8223</int>". It doesn't necessarily have to use sed as long as it gets the job done :) Thanks (9 Replies)
Discussion started by: pcwiz
9 Replies

10. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies
Login or Register to Ask a Question