Replacing data column in text files with Linux command
3 answers
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 to share
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 to share