user3228279
user3228279

Reputation: 73

Shell sed command

I have paths.txt like:

pathO1/:pathD1/
pathO2/:pathD2/
...
pathON/:pathDN/

How can I 'sed' insert ' * ' after each pathOX/ ?

The script is:

while read line
do
    cp $(echo $line | tr ':' ' ')   

done < "paths.txt"

substituted by:

while read line
do
    cp $(echo $line | sed 's/:/* /1')   

done < "paths.txt"

Upvotes: 0

Views: 415

Answers (4)

BMW
BMW

Reputation: 45293

Using pure shell

while IFS=: read -r p1 p2
do
  cp $p1 "$p2"
done < file

Upvotes: 0

Kent
Kent

Reputation: 195199

quick and dirty awk one-liner without loop to do the job:

awk -F: '$1="cp "$1' paths.txt

this will output:

cp /home/Documents/shellscripts/Origen/* /home/Documents/shellscripts/Destino/
cp /home/Documents/shellscripts/Origen2/* /home/Documents/shellscripts/Destino2/
...

if you want the cmds to get executed:

awk -F: '$1="cp "$1' paths.txt|sh

I said it quick & dirty, because:

  • the format must be path1:path2
  • your path cannot contain special letters (like space) or :

Upvotes: 0

Ashish Gaur
Ashish Gaur

Reputation: 2050

while read line
do
    path=`echo "$line" | sed 's/:/ /g'`
    cmd="cp $path"
    echo $cmd
    eval $cmd        
done < "./paths.txt"

Upvotes: 0

user3139488
user3139488

Reputation:

This looks to be a similar question to which you asked earlier: Shell Script: Read line in file

Just apply the trick of removing additional '*' before appliying tr like:

cp $(echo $line | sed 's/\*//1' | tr ':' '* ')

Upvotes: 1

Related Questions