将文件内容复制到其他位置

copy the content of the file to other location

我有一个包含函数输出结果的文本文件(路径行和结果正下方),我只想复制结果,下面提到的每个路径行到 name= 中提到的文件....并想粘贴到文件的最后一列

我的数据保存在list.txt

<_io.TextIOWrapper name='/home/liu/datalist/20180603_190202_5.0_38_CD.txt' mode='r' encoding='UTF-8'>
0.9821305886510053
0.9822639336394542
0.9820650754926169
0.9826369946635649
0.983048680372995
<_io.TextIOWrapper name='/home/liu/datalist/20150603_190202_12.0_36_CD.txt' mode='r' encoding='UTF-8'>
0.984263067046808
0.9821305886510053
0.9822639336394542
0.9820650754926169
0.9826369946635649
0.983048680372995
<_io.TextIOWrapper name='/home/liu/datalist/20120603_190202_9.0_35_CD.txt' mode='r' encoding='UTF-8'>
0.984263067046808
0.982871209110385
0.9830143471771022
0.9860952410409616
0.9897257569597108
<_io.TextIOWrapper name='/home/liu/datalist/20100603_190202_125.0_36_CD.txt' mode='r' encoding='UTF-8'>
0.9860794488788789
0.9838808626335948
0.9829656043793615
0.9868238026934462
0.9972756725839034
0.9882482212913676
<_io.TextIOWrapper name='/home/liu/datalist/20240603_190202_265.0_36_CD.txt' mode='r' encoding='UTF-8'>
0.9856758910581078
0.9849158515561436
0.9838016078370099
0.9854127382758501
0.9880763165814402

我尝试了下面的代码:没有得到正确的想法this.Hope一些专家会帮助me.Thanks

#!/bin/sh
for file in `list.txt`
cp cat|list.txt > name

详细说明: 假设我们将 20180603_190202_5.0_38_CD.txt 文件带到 consideration.This 命名文件已经存在于 /home/liu/datalist 目录中并且它包含类似

的文件
1 2 3 4 5
5 6 7 8 9
3 4 5 6 7
3 4 5 6 7 
4 5 6 7 8

我希望文件如下所示

1 2 3 4 5 0.9821305886510053
5 6 7 8 9 0.9822639336394542
3 4 5 6 7 0.9820650754926169
3 4 5 6 7 0.9826369946635649
4 5 6 7 8 0.983048680372995

编辑:新代码,修复旧的丑陋代码:D

结果

$ cat 20150603_190202_12.0_36_CD.txt 
1 2 3 4 5
5 6 7 8 9
3 4 5 6 7
3 4 5 6 7
4 5 6 7 8

$ cat 20180603_190202_5.0_38_CD.txt 
1 2 3 4 5
5 6 7 8 9
3 4 5 6 7
3 4 5 6 7
4 5 6 7 8

$ ./test.sh 

$ cat 20150603_190202_12.0_36_CD.txt 
1 2 3 4 5 0.984263067046808
5 6 7 8 9 0.9821305886510053
3 4 5 6 7 0.9822639336394542
3 4 5 6 7 0.9820650754926169
4 5 6 7 8 0.9826369946635649

$ cat 20180603_190202_5.0_38_CD.txt 
1 2 3 4 5 0.9821305886510053
5 6 7 8 9 0.9822639336394542
3 4 5 6 7 0.9820650754926169
3 4 5 6 7 0.9826369946635649
4 5 6 7 8 0.983048680372995

代码

$ cat test.sh 
#!/bin/bash

cat list.txt | cut -d '/' -f5- | sed "s/' mode='r' encoding='UTF-8'>//" > new_list.txt

while read -r line
do
  if [[ "$line" =~ ".txt" ]]; then
    count=0
    filename="$line"
    touch "$filename"
  else
    count=$(($count+1))
    originalline=$(sed -n ${count}p "$filename")
    sed -i "0,/^$originalline$/s//$originalline $line/" "$filename"
  fi
done < "new_list.txt"

rm new_list.txt

另一个使用关联数组的解决方案:

#!/bin/bash

declare -A filetemp

while read -r line
do
    if [[ $line =~ "name='"(.*?.txt) ]]
    then
        name="${BASH_REMATCH[1]}"
        filetemp[$name]=$(mktemp)
    else
        [ -n "$name" ] && echo "$line" >> "${filetemp[$name]}"
    fi
done < list.txt

for name in "${!filetemp[@]}"
do
    paste -d ' ' "$name" "${filetemp[$name]}" > "${name}.tmp"
    mv -f "${name}.tmp" "$name"
done

编辑:忘记了这一点,但以防万一,最好在不再需要 mktemp 创建的临时文件后清理它们:

for name in "${!filetemp[@]}"
do
    tempfile="${filetemp[$name]}"
    paste -d ' ' "$name" "$tempfile" > "${name}.tmp"
    mv -f "${name}.tmp" "$name"
    rm -f "$tempfile"
done

编辑 2:正如@lucasgvarela 所指出的,当 EXIT 信号被触发时,trap 命令可能是一种更优雅的删除临时文件的方法:

trap 'rm -f "${filetemp[@]}"' EXIT

for name in "${!filetemp[@]}"
do
    paste -d ' ' "$name" "${filetemp[$name]}" > "${name}.tmp"
    mv -f "${name}.tmp" "$name"
done