I have an ASCII file with the following structure:
file1.png otherfile1.png
file2.png otherfile2.png
file3.png otherfile3.png
...
I want to replace .png
with .mat
, but only for the second column. The result should be like this:
file1.png otherfile1.mat
file2.png otherfile2.mat
file3.png otherfile3.mat
...
How do I do that in Bash?
Well, if it is the end of the line...
s/old/new/
search and replace\.
literal dot (without the escape it matches any character)$
end of lineOr to explicitly specify the second column, you could use an
awk
way...gsub(old, new, where)
search and replace$2
second columnYou can replace all
.png
strings directly at the end of a line inINPUTFILE
like this:The command above will not modify
INPUTFILE
but only print the changed version to the terminal.To directly edit the file in place, add the
-i
flag tosed
(or-i.bak
to store a backup of the original file):