difficult sed command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users difficult sed command
# 1  
Old 09-28-2006
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  
Old 09-28-2006
try:
sed 's/\(.*\)/\1\nextra\1/'
# 3  
Old 09-28-2006
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  
Old 09-28-2006
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  
Old 09-28-2006
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  
Old 09-28-2006
check that the \ is in front on the n

sed 's/\(.*\)/\1\nextra\1/'
# 7  
Old 09-28-2006
Yeah it definately is!
I copied your code exactly.
Ive been looking around and there are a few people with the same problem, apparently \n doesnt work on some versions of UNIX.
Do you know of any other commands that will do the same job?

Cheers,
Dave
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