sed: appending alternate line after previous line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: appending alternate line after previous line
# 1  
Old 02-10-2009
sed: appending alternate line after previous line

Hi all,
I have to append every alternate line after its previous line. For example if my file has following contents
line 1: unix is an OS
line 2: it is open source
line 3: it supports shell programming
line 4: we can write shell scripts

Required output should be
line1: unix is an OS it is open source
line 2: it supports shell programming we can write shell scripts

I think this could be done using sed, but how could i do this please help?
If it cant be done using sed then please suggest other methods
# 2  
Old 02-10-2009
u can use the record feature of vi ( vim).
its very easy. pl let us knw if u cudnt find how to use it.

it is very useful when we dont have sed/awk expertise.
# 3  
Old 02-10-2009
Hi anchal,
thanks for replying
Actually i m very new to unix, therefore not able to figure it out how to do that. Can u please help me how could i do that?
Thanks
# 4  
Old 02-10-2009
Looks like homework... a bit... and posting homework/classroom stuff here is forbidden.. nevertheless. Your part is to look up how it works Smilie

Code:
root@isau02:/data/tmp/testfeld> cat infile
unix is an OS
it is open source
it supports shell programming
we can write shell scripts
root@isau02:/data/tmp/testfeld> sed 'N; s/\n/ /' infile
unix is an OS it is open source
it supports shell programming we can write shell scripts

# 5  
Old 02-10-2009
zaxxon's solution is the best one...
Without using sed i tried like this ( its a lengthy code )

#!/bin/bash
Count=0
while read line
do
echo $line >> output.txt
Count=`expr $Count + 1`
if [ $Count -eq 2 ]
then
{
awk -v RS='' '{gsub("\n", FS)}1' output.txt >> new.txt
:>output.txt
Count=0
}
fi
done < testfile.txt
rm -rf output.txt



--------output----------

line 1: unix is an OS line 2: it is open source
line 3: it supports shell programming line 4: we can write shell scripts
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed for appending a line before a pattern and after another patter.

Hi All, I'm appending a line before a pattern and after another pattern(;) but omitting the first pattern - same as if comes duplicates before the second pattern. Also, I'm adding a word before the first pattern - Here is my file select blah blah hello select blah blah ; select... (6 Replies)
Discussion started by: Mannu2525
6 Replies

2. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

3. Shell Programming and Scripting

Inserting a line in a file after every alternate line

Friends , I have a large file and i need to insert a line after every line.I am actually unaware how to do it.Any help appreciated. My File control station *ATM* , qread $OSS.Jul13A.FI01 interval 1 intcount 1 control station *ATM* , qread $OSS.Jul13A.FI02 interval 1 intcount... (4 Replies)
Discussion started by: appu2176
4 Replies

4. Shell Programming and Scripting

Sed: Working on a line Previous to a pattern.

Hello everyone, I am working with some train time tables, and i have hit a bit of a road block. Using grep/sed i have done a reasonable job of parsing the html into comma delimited format, but NJ transit prints The Track number and status on a new line, and I would much prefer it all on a... (6 Replies)
Discussion started by: mussen
6 Replies

5. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

6. Shell Programming and Scripting

SED or AWK "append line to the previous line"

Hi, How can I remove the line beak in the following case if the line begin with the special char “;”? TEXT Text;text ;text Text;text;text I want to convert the text to: Text;text;text Text;text;text I have already tried to use... (31 Replies)
Discussion started by: research3
31 Replies

7. Shell Programming and Scripting

Print previous, current and next line using sed

Hi, how can i print the previous, current and next line using sed? current line is the matching line. The following prints all lines containing 'Failure' and also the immediate next line cat $file | sed -n -e '/Failure/{N;p;}' Now, i also want to print the previous line too. Thanks,... (8 Replies)
Discussion started by: ysrinu
8 Replies

8. Shell Programming and Scripting

Merging previous line using sed

I've been trying to merge a previous line when I find a pattern using sed with no luck. Maybe someone can help me. I am trying to find </Endtag>. Once I find that, I want to merge the previous line with the current line. I can do it with the next line with using N in sed but not previous.... (6 Replies)
Discussion started by: quixoticking11
6 Replies

9. Shell Programming and Scripting

Sed - Appending a line with a variable on it

Hi, I searched the forum for this but couldn't find the answer. Basically I have a line of code I want to insert into a file using sed. The line of code is basically something like "address=1.1.1.1" where 1.1.1.1 is an IP Address that will vary depending on what the user enters. I'll just refer... (4 Replies)
Discussion started by: eltinator
4 Replies

10. Shell Programming and Scripting

Appending line/lines with sed

Hi folks, I need to append line or bulk of lines into a file. For example,I have the following section in opmn.xml file: <process-type id="OC4J_RTEadmin_NIR" module-id="OC4J"> <module-data> <category id="start-parameters"> <data... (28 Replies)
Discussion started by: nir_s
28 Replies
Login or Register to Ask a Question