Replacing data column in text files with Linux command

I have several text files with lines separated by tabs. The second column contains invalid data. How can I change everything in the second column to a specific text string?

+2


source to share


3 answers


awk ' { $2="<STRING>"; print } ' <FILENAME>

      



+3


source


cat INFILE | perl -ne '$ln=$_;@x=split(/","/); @a=split(/","/, $ln,8);@b=splice(@a,0,7);  $l=join("\",\"", @b); $r=join("\",\"", splice(@x,8)); print "$l\",\"10\",\"$r"'

      



This is an example that changes the 10th column to "10". I prefer this since I don't have to count the matching parentheses like in the sed method.

+2


source


Simple and cheap hack:

 cat INFILE | sed 's/\(.*\)\t\(.*\)\t\(.*\)/\1\tREPLACEMENT\t\3/' > OUTFILE

      

testing:

 echo -e 'one\ttwo\tthree\none\ttwo\tthree' | sed 's/\(.*\)\t\(.*\)\t\(.*\)/\1\tREPLACEMENT\t\3/'

      

takes

 one    two three
 one    two three

      

and produces

 one    REPLACEMENT three
 one    REPLACEMENT three

      

+1


source







All Articles