tags:

views:

72

answers:

2

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
+3  A: 
awk ' { $2="<STRING>"; print } ' <FILENAME>
biznez
Beat me to it. Just trying to validate as your answer came through :-)
Chris J
I think that will split on any whitespace, not just tabs. Adding a -F"\t" might help.
Nate Kohl
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
If you're using bash, you could use environment variables. Like: export var="foo.txt" awk -F"\t" '{$2="'$var'"; print}' $var
Nate Kohl