It's not clear what head -3 *.txt | awk '{print $4}'
returns here since you haven't shown us a sample of your file(s), however sed -i 's/GLN/LOO/g' *.txt
will replace all of the instances of GLN in all the lines in all the files matching *.txt
, provided the loop executes at least once.
AFAIK there's no direct way to address the last n lines of a file in sed - so to use that, you would need to calculate the offset externally, using wc -l
and shell arithmetic for example:
for f in *.txt; do
start=$(( $(wc -l <"$f") - 9 ))
sed "$start"',$s/GLN/LOO/g' "$f"
done
(I removed the -i
so output goes to the terminal, for testing). You could use something like awk 'END{print NR-9}' "$f"
in place of $(( $(wc -l <"$f") - 9 ))
to get the start offset if your intention was to use awk.
Alternatively you could use tac
to invert the file and make replacements in the first n lines, then tac
the result - although that makes in-place replacement complicated.
In this situation I would probably reach for ed
or ex
which do support numerical address offsets ex. testing with output to the terminal:
for f in *.txt; do
printf '%s\n' '$-9,$s/GLN/LOO/g' ',p' | ed -s "$f"
done
Once you are happy that it's doing the right thing, change ,p
to wq
to write the result to the file (equivalent of sed's -i
).