The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Finding difficult in installing gcc-3.4.6 complier on Solaris 10. susainaj SUN Solaris 4 07-11-2008 11:35 AM
so difficult question about using grep shrimpy UNIX for Dummies Questions & Answers 2 03-22-2008 12:19 AM
A difficult script (for me) DNAx86 Shell Programming and Scripting 2 01-07-2008 04:04 PM
difficult time differences Lokesha SUN Solaris 1 06-20-2006 11:42 AM
Difficult Filtering Problem Piyush UNIX for Advanced & Expert Users 1 06-30-2003 05:06 PM

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 09-28-2006
Dave724001 Dave724001 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 24
difficult sed command

Im trying to replace every line

line1
line2
line3

with:

line1
extraline1

line2
extraline2

line3
extraline3


with about 10 extra lines
I am able to add axtra lines with:

sed '/[^ ]*/ a\\
new line\\
' file > newfile

but i want the name of the original line in the new lines,
I have tried using '&' eg:

sed '/[^ ]*/ a\\
extra&\\
' file > newfile

but it outputs the & symbol and not the original line!

Can anyone help me?
  #2 (permalink)  
Old 09-28-2006
roblrob roblrob is offline
Registered User
  
 

Join Date: May 2006
Location: St. Petersburg, Fl
Posts: 5
try:
sed 's/\(.*\)/\1\nextra\1/'
  #3 (permalink)  
Old 09-28-2006
Dave724001 Dave724001 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 24
Thanks, that works for the first line, but i want to create a second line with the first line in it. And the following code doesnt work.

sed -e 's/\(.*\)/\1\nextra\1/' -e '/\(.*\)/ a\\
nextra\1\nextra/1/\\
' test > temp

The result is:

filename1.ADFnextrafilename1.ADF
nextra1nextra/1/

Can you help?
  #4 (permalink)  
Old 09-28-2006
roblrob roblrob is offline
Registered User
  
 

Join Date: May 2006
Location: St. Petersburg, Fl
Posts: 5
Hmmm:

file:
line1
line2
line3

cat file|sed 's/\(.*\)/\nextra\1/'>newfile

cat newfile
line1
extraline1
line2
extraline2
line3
extraline3

Maybe I don't understand what you actually want to do.
  #5 (permalink)  
Old 09-28-2006
Dave724001 Dave724001 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 24
Yeah thats exactly what I want!
But for some reason, when I run it, i get:

file:
line1
line2
line3

cat file|sed 's/\(.*\)/\nextra\1/'>newfile

cat newfile:
nextraline1
nextraline2
nextraline3

Do you have any idea why?
  #6 (permalink)  
Old 09-28-2006
roblrob roblrob is offline
Registered User
  
 

Join Date: May 2006
Location: St. Petersburg, Fl
Posts: 5
check that the \ is in front on the n

sed 's/\(.*\)/\1\nextra\1/'
  #7 (permalink)  
Old 09-28-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
Quote:
Originally Posted by Dave724001
Im trying to replace every line

line1
line2
line3

with:

line1
extraline1

line2
extraline2

line3
extraline3


with about 10 extra lines
I am able to add axtra lines with:

sed '/[^ ]*/ a\\
new line\\
' file > newfile

but i want the name of the original line in the new lines,
I have tried using '&' eg:

sed '/[^ ]*/ a\\
extra&\\
' file > newfile

but it outputs the & symbol and not the original line!

Can anyone help me?


Alternative in Python:

Code:
>>> extralines = ['extraline1','extraline2','extraline3']
>>> lines = ['line1','line2','line3']
>>> zip(lines,extralines)
[('line1', 'extraline1'), ('line2', 'extraline2'), ('line3', 'extraline3')]
>>> for i in zip(lines,extralines):
... 	print '\n'.join(i)
... 	
line1
extraline1
line2
extraline2
line3
extraline3

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 10:28 PM.


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