I need to replace a string in a (large) file on a MacOSX 10.10. My file looks like this:
Y16-TUL-SUB_ Y16-TUL-SUB_ Y16-TUL-SUB_ Y16-TUL-SUB_ Y16-TUL-SUB-
Y16-TUL-SUB_ Y16-TUL-SUB_
and I need to replace Y16_TUL_SUB_ with Y16-TUL-SUB-. File name could be test.txt.
I have tried a lot of the different suggestions with sed, awk and python. E.g this:
#!/usr/bin/env python import sys import os import tempfile
tmp=tempfile.mkstemp()
with open(sys.argv[1]) as fd1, open(tmp[1],'w') as fd2:
for line in fd1:
line = line.replace('Y16_TUL_SUB_','Y16-TUL-SUB-')
fd2.write(line)
os.rename(tmp[1],sys.argv[1])
or sed supposedly for mac
or find:
find . -type f -name test.txt | xargs sed -i ""
"s/Y16_TUL_SUB_/Y16-TUL-SUB-/g'
or sed:
sed -i -e "s/Y16_TUL_SUB_/Y16-TUL-SUB/g" test.txt
or awk
awk '{gsub(/Y16_TUL_SUB_/,"Y16-TUL-SUB")}' test.txt
All these commands have run and returned empty output files, or not changed anything in the original files anyway.
What am I doing wrong?