简体   繁体   中英

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"

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 ':' '* ')
while read line
do
    path=`echo "$line" | sed 's/:/ /g'`
    cmd="cp $path"
    echo $cmd
    eval $cmd        
done < "./paths.txt"

Using pure shell

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

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 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM