I have two files, each with data arranged in two columns. Columns are separated by a semicolon. The first file (11_19.txt) includes more lines and the second file(12_19.txt) is to update the first file. Each line has an id in the first part, so the script should update the first file with the data from the second file if it finds a line with the same id in the both files.
Lets say that first file is like this :
$ cat 11_19.txt
id=123;112233
id=456;445566
id=789;778899
id=000;000000
and the second file is like this :
$ cat 12_19.txt
id=123;123123
id=000;999999
Expected outcome of script :
$ ./script.sh 11_19 12_19
11_19
id=123;123123
id=456;445566
id=789;778899
id=000;999999
I tried turning parts of csv into an array but can't make it work.
id=($(cut -f1 -d, $2))
info=($(cut -f2 -d, $2))
for id in ${id[@]}; do
if grep "$id" $1; then
sed -E s/id="$id";.*/id="$id";$info"/ $1>$1
else
:
fi
done
Secondly, I want to use the second file to change the values of a different file with a different extension, lets say an html file, and add the second value in the second file where the id matches in the html file.
<!DOCTYPE html>
<html>
<body>
...
<p1 id=123></p1>
Expected output:
<!DOCTYPE html>
<html>
<body>
...
<p1 id=123>123123</p1>
joinwould work if the files are appropriately sorted - aside from the field separator, this is essentially the same as updating one file based on values in another with AWK i.e. something likeawk -F';' 'BEGIN{OFS=FS} NR==FNR{a[$1] = $2; next} $1 in a {$2 = a[$1]} {print}' 12_19.txt 11_19.txt