Operating System - Linux
1819763 Members
3245 Online
109606 Solutions
New Discussion юеВ

Will "sed" do what I need?

 
SOLVED
Go to solution
Yvonne Butler
Regular Advisor

Will "sed" do what I need?

Hello

I'm writing a script which creates a file containing a list of files with their paths. What I now need my script to do is create another file which will add "cp" before the path/filename and then add "path" at the end of each line within the first file. I think "sed" might do what I need but its been so long since I've used this command I've forgotten how. Any ideas anyone please?
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: Will "sed" do what I need?

I think awk might be easier. Something like:

{ print "cp", $0, "path" }

I think.


Pete

Pete
Eric SAUBIGNAC
Honored Contributor

Re: Will "sed" do what I need?

Bonsoir Yvonne,

Assuming your file YourInputFile contains one absolute path to a file per line you could do like this and obtain what you want in YourOutputFile :

sed 's/^/cp /' < YourInputFile | sed 's/$/ path/' > YourOutputFile

Regards

Eric
James R. Ferguson
Acclaimed Contributor

Re: Will "sed" do what I need?

Hi:

I would think by saying "then add 'path' at the end..." you may mean add the DIRECTORY path in which the file was found. Thus, '/usr/bin/echo' yields '/usr/bin'. If so, you could process with:

#!/usr/bin/sh
while read LINE
do
MYPATH=$(dirname $LINE)
echo cp ${LINE} ${MYPATH}
done < file.in > file.out

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Will "sed" do what I need?

Hi (again) Yyonne:

Duh, I think you mean:

# sed -e 's!\(.*\)!cp \1 /newpath!' file

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Will "sed" do what I need?

>Eric: sed 's/^/cp /' < YourInputFile | sed 's/$/ path/'

You can optimize this to one sed with two scripts:
sed -e 's/^/cp /' -e 's:$: path:' YourInputFile
Yvonne Butler
Regular Advisor

Re: Will "sed" do what I need?

Thanks to you all for your suggestions. I've now put together the sed command below to do all of what I need:

sed -e '/^$/d' -e 's!\(.*\)!host cp \1 /stage/dst/backup!' /tmp/dst.bkfiles > /tmp/dst_bkfiles.sql

I now get output into a new file showing something like the following:

host cp /redo1/dst/redo05a.log /stage/dst/backup
host cp /redo2/dst/redo05b.log /stage/dst/backup

This does leave me with lots of blank spaces between the first and second path, but that shouldn't stop the script from running. Will find out tonight when I try it on a live system. If anyone knows how to remove numerous blank spaces between the two paths though, I'd be glad to hear from you.

Many thanks to you all again though for reminding me how to use SED!!!!
Eric SAUBIGNAC
Honored Contributor
Solution

Re: Will "sed" do what I need?

Bonjour Yvonne,

I dont see those lots of blanks spaces, unless you have them in the input file

Anyway you can add this filter :

-e 's! *! !g' ==> be aware that there are 2 space characters : it means one space followed by any number of spaces.

Your command will become :

sed -e '/^$/d' -e 's!\(.*\)!host cp \1 /stage/dst/backup!' -e 's! *! !g' /tmp/dst.bkfiles > /tmp/dst_bkfiles.sql

Regards

Eric
Yvonne Butler
Regular Advisor

Re: Will "sed" do what I need?

Thanks Eric, that's tidied up my output file perfectly.
Yvonne Butler
Regular Advisor

Re: Will "sed" do what I need?

See above.

Many thanks everyone!