awk - How to compare two files that are partially matching and replace the parts that are matching in bash? -
i have 2 dat files, file1 , file2. first 1 has (for example) 1000 lines , second 100 lines. lines in both files have same format, i.e. 5 numbers, separated space. lines values of file2 partially matching lines file1. example, there line in file2 is:
1 2 3 4 5
and there line in file1 is:
1 2 3 9 5
it 4th value not matching. 1st, 2nd, 3rd , 5th values match. want in bash script. have started script far.
#! /bin/bash #usage: sh ./script.sh test.dat check.dat test_file=$1 check_file=$2 ## each line in test_file while read line ; arr=$(echo $line | tr " " "\n") part_match=$(echo ${arr:0:8}) x=$(grep "$part_match" ${check_file}) ## here, if partial match exists in file2, save line in x. ## try substitute them awk or sed none of them works. awk '{gsub(/${x}/, "${line}")}' check.dat > check_new.dat ## or sed -i "s/$x/$line/pw" check.dat done < ${test_file}
none of them works. doing wrong? thank much!
"if partial match exists in file 2" - based on 0:8
, if have partial match, have full match, because include 4th digit.
if want match on first 3 digits, should using 0:6
.
the sed command not correct - either remove w
flag or specify file name. edit original check.dat in place:
sed -i "s/$x/$line/p" check.dat
or, write change check_new.dat, keeping check.dat
sed "s/$x/$line/pwcheck_new.dat" check.dat
Comments
Post a Comment