I have several text files whose lines are tab-delimited. The second column contains incorrect data. How do I change everything in the second column to a specific text string?
+1
A:
A simple and cheap hack:
cat INFILE | sed 's/\(.*\)\t\(.*\)\t\(.*\)/\1\tREPLACEMENT\t\3/' > OUTFILE
testing it:
echo -e 'one\ttwo\tthree\none\ttwo\tthree' | sed 's/\(.*\)\t\(.*\)\t\(.*\)/\1\tREPLACEMENT\t\3/'
takes in
one two three
one two three
and produces
one REPLACEMENT three
one REPLACEMENT three
Peter
2009-09-14 21:05:58
Beat me to it. Just trying to validate as your answer came through :-)
Chris J
2009-09-14 21:07:57
I think that will split on any whitespace, not just tabs. Adding a -F"\t" might help.
Nate Kohl
2009-09-14 21:11:27
How would I supply an argument to the command above. Example, if STRING and FILENAME were the same, could I pipe the value as an argument to the command?
biznez
2009-09-14 21:13:16
If you're using bash, you could use environment variables. Like: export var="foo.txt" awk -F"\t" '{$2="'$var'"; print}' $var
Nate Kohl
2009-09-14 21:18:57