Hi,
I need to replace a character in a specific position in only the first line of a
file (actually many files). Change T to a P in position 103.
I'm on solaris and my implementation of sed doesn't have the -r capability.
Thx,
Tim
Hi,
I need to replace a character in a specific position in only the first line of a
file (actually many files). Change T to a P in position 103.
I'm on solaris and my implementation of sed doesn't have the -r capability.
Thx,
Tim
one way:
# assumes pos 103 is always a T -> P change
for fname in $( find . -name 'filetofix*')
do
awk '{if(FNR==1)
{printf("%s%s%s", substr($0,1,102), "P", substr($0,104) )}
else
{print} ' $fname > ${fname}.fixed
done
$ cat > t
testing...
test of second line.
$ sed -e '1s/\(.\{4\}\).\(.*\)/\1P\2/' t
testPng...
test of second line.
If you wanted the 103rd character to be replaced, then use 102 instead of 4 in the sed command.
sed -e '1s/\(.\{102\}\).\(.*\)/\1P\2/' t
And if you have the -i option, then you can use it which will edit the file itself.
sed -ie '1s/\(.\{102\}\).\(.*\)/\1P\2/' t
Thanks! I couldn't get the awk to work, but the sed command did the trick.
Tim
Try this awk:
awk 'BEGIN{FS=OFS=""} {if (NR==1&&$103="T") $103="P" }1' input.txt
If you have the -r option:
sed -r '1s/^(.{102})T/\1P/' infile
otherwise
sed '1s/^\(.\{102\}\)T/\1P/' infile
The ^ caret is important otherwise you may end up matching some T in a different column > 102
this will work
awk 'BEGIN{FS=OFS=""} {if (NR==1&&$103="T") $103="P" }1' file.txt
Have a look two posts above yours.. (but welcome to the forum )
The OP is using Solaris, so unless GNU awk is available, setting FS to an empty string will not split on characters.