sed command to condense two lines into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to condense two lines into one
# 1  
Old 02-07-2013
sed command to condense two lines into one

My eyes glaze over trying to understand multiline sed commands, so please forgive me if this has been asked before. I'd like to condense the following two lines:
Code:
fooPATTERN1
PATTERN2bar

into the following one line:
Code:
foobar

How is that done in sed?

Last edited by Scrutinizer; 02-07-2013 at 04:28 PM.. Reason: quote tags -> code tags
# 2  
Old 02-07-2013
Code:
awk '{ gsub(/[A-Z]|[0-9]/,x); ORS=""; }1' file

OR
Code:
awk '{ gsub(/PATTERN1|PATTERN2/,x); ORS=""; }1' file


Last edited by Yoda; 02-07-2013 at 04:11 PM.. Reason: added
# 3  
Old 02-07-2013
sed:
Code:
sed 'N;s/PATTERN1\nPATTERN2//' infile


Last edited by rdrtx1; 02-07-2013 at 04:03 PM.. Reason: dang! copied from old post... removed not end with 0 exp
# 4  
Old 02-07-2013
Code:
sed 'N;s/PATTERN1\nPATTERN2//' file

Hey rdrtx1, what is the
/0$/! at the beginning of your expression?
# 5  
Old 02-07-2013
I stumbled over this as well... /0$/ ... pls explain!
All above proposals work on exactly the sample file. If you use an extended version, lines above and below the pattened ones, they run into trouble. Pls try this on an extended file and report back:
Code:
$ sed ':Start; N;s/PATTERN1\nPATTERN2//;P;D; T Start' file

It tries to read each line appending it to the previuous, tests against pattern1\npattern2, prints/deletes previous line, reads next...
# 6  
Old 02-07-2013
Code:
sed '/PATTERN1/{N;s/PATTERN1\nPATTERN2//;}' infile

# 7  
Old 02-07-2013
That takes the lead - short, sharp, and to the point; again. Congratulations!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command removes lines in file

Hi, I am modifying a file with sed command. i want to make SCORE= blank in the file whereever SCORE=somevalue. What will *$ do in the below command? cat $file | sed 's/SCORE=.*$/SCORE=\r/g' > newfile The last line is also missing in the new file. How to make SCORE='100' to SCORE=... (5 Replies)
Discussion started by: ashok.k
5 Replies

2. Shell Programming and Scripting

sed command : print lines having no # at the begining

I am trying to print those line which has no # in the begining of the line. The sed I used for this purpose as shown below is not giving the required output. echo 'PDE 5600' | sed -n 's/^\!#/&/p' Where lies the problem:confused: (3 Replies)
Discussion started by: hiten.r.chauhan
3 Replies

3. Shell Programming and Scripting

What's the command to remove empty lines with sed?

3 10 20 10 100 100 10000 Output: 3 10 20 10 100 100 10000 ---------- Post updated at 07:59 AM ---------- Previous update was at 07:56 AM ---------- sed '/^$/d' file doesn't work. (8 Replies)
Discussion started by: cola
8 Replies

4. Solaris

sed command to print lines after expression

Hi guys. I need a sed command to print like 10 lines after a regular expression is found in the log. Can anyone help me out. Thanks ---------- Post updated at 10:52 AM ---------- Previous update was at 10:34 AM ---------- never mind. I just did the search bewteen two expressions. (1 Reply)
Discussion started by: jamie_collins
1 Replies

5. UNIX for Dummies Questions & Answers

sed command, make multiple lines into one

:confused:Hi, I'm relativley new at unix so am having difficulties at the most basic of areas. I am trying using sed to make multiple lines into one line. For example, i would like: Mary had a little lamb to look like this Maryhadalittlelamb so far i have tried sed... (1 Reply)
Discussion started by: cavanac2
1 Replies

6. Shell Programming and Scripting

How to use sed command for change special lines?

Hi, I want to add a character "#" in this few lines with sed command Initial: ### CACCIA: DEBUT ### if $(grep -wqi "$2" /etc/passwd); then chuser -R files registry=files $2 fi ### CACCIA: FIN ### Result with sed command: ### CACCIA: DEBUT ### #if $(grep -wqi "$2"... (4 Replies)
Discussion started by: khalidou13
4 Replies

7. UNIX for Dummies Questions & Answers

Sed command over multiple lines in csh??

hi i have a long sed command in a csh script that won't fit on 1 line. how do i break it up correctly over multiple lines? this doesn't seem to work in csh: sed -e s/template/$IP.$NN/ \ -e s/NRG/6/ \ -e s/inputf/$IS.$NN/ \ -e s/SHIFT/10.0/ <template.egsinp > $IP.$NN.inp i get: sed:... (1 Reply)
Discussion started by: tuathan
1 Replies

8. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

9. Shell Programming and Scripting

sed command to get the total lines

hi, I am using the following code to get the first field of all the lines in a file using sed command.But its not printing the last line.Why it is so and how can i read the different fields of each line ? code: for i in $(sed = filename.txt | sed 'N;s/\n/ /' | cut -d " " -f1) do val1=`sed... (5 Replies)
Discussion started by: Sharmila_P
5 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