biznez
biznez

Reputation: 4021

Replacing a column of data in text files with Linux command

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?

Upvotes: 2

Views: 7518

Answers (3)

user1706991
user1706991

Reputation: 407

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 as I don't have to count the matching parenthesis like in the sed technique.

Upvotes: 2

Peter
Peter

Reputation: 132197

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

Upvotes: 1

biznez
biznez

Reputation: 4021

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

Upvotes: 3

Related Questions