How to use SED to replace a line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use SED to replace a line?
# 1  
Old 06-02-2012
How to use SED to replace a line?

Hi I am a complete novice with sed and want to use sed to replace this line in the input file

/data/folder/date/etc/etc/etc/etc/etc/name

with

/data/folder/date/etc/etc/etc/etc/etc/$name

where I input the variable $name

trouble is is there anyway I don't have to write out the whole line to be replaced (due to the length and that the name various every time so I don't want to have to edit the script everytime, which is the point of writing a script!). So how do I specify the SED command so that it recognises the line beginning with /data/folder/etc and replace only the last variable?

Many thanks
Robert
# 2  
Old 06-02-2012
Hi, try:
Code:
sed '/\/data\/folder\//s/[^/]*$/$&/' file

# 3  
Old 06-02-2012
Thanks scrutiniser. Would you mind explaining what the code means? I am trying to learn thanks so much!
# 4  
Old 06-02-2012
Sure:
Code:
/\/data\/folder\// # If a line contains "/data/folder/",
s/[^/]*$/$&/       # then substitute zero to more (*) characters that are not a "/" ([^/]) at
                   # the end of the line ($), by the same string (&), prepended by a dollar sign ($)

# 5  
Old 06-02-2012
Hi Scrutinizer

Not sure if you have misunderstood me or I still getting myself confused.

I am trying to replace a line in a file where it contains name of the last folder (folder name 1) with a long pathname (see eg in first thread), and I want to write a script containing a SED command where I specify the name of the new folder which is to replace (folder name 1).

The point of the sed command is folder name 1 is different everytime I use the script, it only needs to recognise that line and replace folder name 1 with the new folder name which I input when I call in the script.

Thanks, I am quite clueless with SED at the moment.
# 6  
Old 06-02-2012
You need to be a bit more specific I'm afraid... could you show a representative sample of input, desired output, attempts at a solution and specify what OS and version you are using?
# 7  
Old 06-02-2012
I am using zsh as the shell

in my file test.txt there is a line which read

/data/folder1/folder2/folder3/folder4/folder5/folder name1

and I want to write a script file containing SED command which reads in test.txt and replace folder name1 with a new folder name that I specify when I call in the SED, something like

new_folder_name=ABCD
sed s:/data/folder1/folder2/folder3/folder4/folder5/folder name1:/data/folder1/folder2/folder3/folder4/folder5/$new_folder_name

as a result the script will replace that line in test.txt with the new folder name ABCD.

Trouble is folder name 1 would be different everytime I call the scripts, so to avoid having to edit the SED command everytime is there a generic way I can write the SED command so that it only need to recognises that line in the file and replace the last folder name with the new folder name I specify when I execute the script? New folder name would be used as a variable here.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

Replace line with sed on OS X

So, I want to, in a script, do an in-line replacement of a line in a file. Piece of cake in Linux with GNU sed. With OSX and BSD sed, not so much. I've been Googling, but nothing is really helping... either an answer is for a use case different enough from what I want to do, or it's something... (10 Replies)
Discussion started by: jnojr
10 Replies

6. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

7. Shell Programming and Scripting

sed to do replace on line above

Hello, I need to use sed to find a certain string and then do a replace on the line above. For example if I had the following text: AAAAAAA BBBBBBB CCCCCCC DDDDDD I would like to find CCCCCCC, but I would like the replace to be carried out on BBBBBBB. I have done some... (11 Replies)
Discussion started by: toptiger
11 Replies

8. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

9. Shell Programming and Scripting

sed to replace <cr><cr><cr> in a line with <cr>

Hi, Please help me with the sed command for the following scenario: I've in abc.txt, asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr> I want a sed command to do asdasdada<cr>aasdsad<cr>asdasdfsdfs<cr>asdsada<cr>adasd<cr>daasdaasd<cr> ... (13 Replies)
Discussion started by: skpvalvekar
13 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question