copy the content of the file to other location

Viewed 74

i have a text file that contain output results of a function(path line and just below the results) and i want to copy only the results, mentioned below of each path line to the file that is mentioned in name=.... and want to paste at the last column of the file

my data is here saved in 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

i tried the code below: not getting proper idea to do this.Hope some expert would help me.Thanks

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

Details explanation: suppose we take 20180603_190202_5.0_38_CD.txt file to the consideration.This named file is already present inside /home/liu/datalist directory and it contains file like

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

and i want the file to be as below

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
2 Answers

EDIT: New code, fixing the old ugly code :D

Result

$ 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

Code

$ 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

Another solution using associative arrays:

#!/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

EDIT: Forgot about this but, just in case, it would be a good idea to clean up the temporary files created by mktemp after they are no longer required:

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

EDIT 2: As pointed by @lucasgvarela, the trap command could be a more elegant way of removing the temporary files when the EXIT signal is triggered:

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

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