difficult sed command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users difficult sed command
# 8  
Old 09-28-2006
Just check my HP box and got the same result that you did. Try this on your sed:

sed '/\(.*\)/{G;s/\(.*\)\(\n\)/\1\2extra\1/;}' file

or

echo files|sed '/\(.*\)/{G;s/\(.*\)\(\n\)/\1\2extra\1/;}' >newfile
# 9  
Old 09-28-2006
The first one worked, thank you for all your help!
# 10  
Old 09-28-2006
Sorry if im being stupid, but what would the code be if you wanted to add a third (or more) line to each line of original code? (each new line may contain the original line)

Thanks,
Dave
# 11  
Old 09-28-2006
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

# 12  
Old 09-28-2006
Thanks for the reply, but I cant get that to work!
I was looking for a method which i dont need to type each line out because I need to run it on a file with 800 lines and I want it to do the same thing to each line.
# 13  
Old 09-28-2006
sed '/\(.*\)/{G;s/\(.*\)\(\n\)/\1\2extra\1\2extraextra\1/;}' file
# 14  
Old 09-28-2006
Thank you for that, it works perfectly!
Uve saved me sooo much time!
Cheers!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Difficult in analyzing an algorithm

Hello, I was reading Heuritics text and came across an algorithm below. Finding hard to analyze it can any one help me out below... How to analyze if I take say no. of types are 5 and each type has say 20 coins. thanks. Let {c1, c2...cn=1} be a set of distinct coin types where ci is... (1 Reply)
Discussion started by: sureshcisco
1 Replies

2. UNIX for Dummies Questions & Answers

difficult problem with function declaration

Hello, I have a problem with the declaration of a function. This is how I declare the function : c:63: void foo(threadpool *tp,void (*func)(void*), (void*)arg); Inside main, I call it like this: main(){ .......... threadpool y; c:104: ... (4 Replies)
Discussion started by: garag11
4 Replies

3. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

4. UNIX for Advanced & Expert Users

File alignment and performances... (difficult)

Hello ! I will use my best english possible to explain my objective. I'm french so pardon for the lack of precision... So, what i would like to do in shell script (but you will possibly answer ''not possible in script'' have to use low level langage or something like that) is described below.... (3 Replies)
Discussion started by: Gnaag
3 Replies

5. Shell Programming and Scripting

difficult nawk to understand

I found a command who prints x lines before and after a line who contain a searched string in a text file. The command is : nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=2 a=4 s="string" file1 It works very well but I can't understand the syntax, too difficult with... (2 Replies)
Discussion started by: ahmad.diab
2 Replies

6. UNIX for Dummies Questions & Answers

so difficult question about using grep

en...how to grep some words from some file, the goal is that, we donot want the words are exactly 9 charactor, and want to grep from some words that are longer than 9 and it contain a substring that with 9 different charactors (2 Replies)
Discussion started by: shrimpy
2 Replies

7. Shell Programming and Scripting

A difficult script (for me)

Hi, I'm a beginners, this is one of my first script, it's easy, but I don't know how to write this script: The script receive in input 4 parameters: 1) user_name 2) r and/or w and/or x ( rwx, rw, x, ....) 3) u and/or g and/or o ( u, uo, ugo, ...) 4) the path name The script print a... (2 Replies)
Discussion started by: DNAx86
2 Replies

8. Solaris

difficult time differences

:rolleyes: Hi, How to take the time diffence between start and finish time from a log file? It is like ..... started at Jun 20 23:20 . . ..... finished at Jun 21 01:40 Tryed so many ways but failed to ger exact way. :confused: Your help will be honoured. Ta........Lokesha (1 Reply)
Discussion started by: Lokesha
1 Replies

9. UNIX for Advanced & Expert Users

Difficult Filtering Problem

Sir, I have a file containing say 1000 lines that contain 100 paragraphs of 10 lines each separated by blank lines.I have to match a pattern or a string "hdfhasdjkasdhs" and print the complete paragraphs containing these strings.I can do this with the help of line editor ex,but how can I use... (1 Reply)
Discussion started by: Piyush
1 Replies
Login or Register to Ask a Question