简体   繁体   English

Shell sed命令

[英]Shell sed command

I have paths.txt like: 我有如下的paths.txt:

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

How can I 'sed' insert ' * ' after each pathOX/ ? 我如何在每个pathOX /之后'sed'插入'*'?

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 这似乎是您之前询问的类似问题: Shell脚本:读取文件中的行

Just apply the trick of removing additional '*' before appliying tr like: 只需在应用tr之前应用删除其他“ *”的技巧即可:

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单线无循环执行此工作:

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: 如果要执行cmds:

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

I said it quick & dirty, because: 我说它又快又脏,因为:

  • the format must be path1:path2 格式必须为path1:path2
  • your path cannot contain special letters (like space) or : 您的路径不能包含特殊字母(如空格)或:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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