The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Append line that does not contain pipe to it previous line ainuddin Shell Programming and Scripting 11 11-11-2008 10:58 AM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 10:19 AM
Appending a line in a file after a particular line maxvirrozeito Shell Programming and Scripting 7 12-12-2007 01:58 PM
Appending line ending with '}" to new line aismann Shell Programming and Scripting 4 08-13-2007 03:09 AM
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-21-2007 01:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-10-2009
rish_max rish_max is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 9
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 (permalink)  
Old 02-10-2009
anchal_khare anchal_khare is offline
Registered User
  
 

Join Date: Jun 2007
Location: Mumbai,India
Posts: 325
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 (permalink)  
Old 02-10-2009
rish_max rish_max is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 9
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 (permalink)  
Old 02-10-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,289
Looks like homework... a bit... and posting homework/classroom stuff here is forbidden.. nevertheless. Your part is to look up how it works


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 (permalink)  
Old 02-10-2009
subin_bala subin_bala is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 57
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:56 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0