Read file line by line with awk to replace characters in certain line numbers
2
votes
4
answers
4690
views
I have got this script here. It is supposed to run the loop by reading the file LineNumbers.file line by line (each contains a line number) and then accordingly replacing 0/0 with ./. in the BEFORE_File.txt. It works, but it takes only the very last line of the file LineNumbers.file, instead of the >100 entries.
I am not sure what I am doing wrong here. Can you please help me to get awk to read the LineNumbers.file line by line?
I have got it to work with
sed -i "${line}s/0\/0/\.\/\./" "${myFileTmp}"
, but it was really slow on the >3GB large files that I have got. So I thought that awk would be a faster option.
Many thanks!
cat ./LineNumbers_TEMP/LineNumbers.file | while read line
do
myFileTmp=BEFORE_File.txt
awk -v var=${line} 'FNR==var { sub(/0\/0/, "\.\/\."); print }' "${myFileTmp}" > AFTER_File.txt
done
For example this is how the files look like:
cat ./LineNumbers_TEMP/LineNumbers.file
1
2
5
File.txt before script:
cat BEFORE_File.txt
0/0
0/0
0/1
0/1
0/0
0/0
0/0
This is how the file should look after running the script:
cat AFTER_File.txt
./.
./.
0/1
0/1
./.
0/0
0/0
At the moment I get only this:
./.
Asked by P. HamB
(35 rep)
Apr 24, 2020, 04:45 AM
Last activity: Apr 25, 2020, 02:23 AM
Last activity: Apr 25, 2020, 02:23 AM