📅 2023-08-05
This perl
one-liner changes the first “foo” in the second column to “bar”.
$ perl -lai -n -e '$F[1] =~ s/foo/bar/; print "@F"' <file>
-l
: adds a newline character after every print
statement;-a
: splits each line into columns, like AWK. the columns are stored in the @F
array;-i
: edits the file in-place. a suffix can be specified like this: -i.bak
, and the original file will be backed up as <file>.bak, which is why it’s separated from the -n
option, otherwise the file would be backed up as <file>n;-n
: wraps the code in a while (<>) {}
block, which basically means iterating through every line of every file, or from stdin if no file was specified;-e
: specifies a command.If you want to modify a column in a file, Lain’s got you wired.