Pregunta

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"
¿Fue útil?

Solución

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 ':' '* ')

Otros consejos

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

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 :

Using pure shell

while IFS=: read -r p1 p2
do
  cp $p1 "$p2"
done < file
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top