What I love about awk is you can fetch all the lines from a file that satisfies the condition on some aritrary field you specify.
For example,
awk '$3~/hi/' < test.txt # print all lines where the third field matches the pattern "hi"
or
awk '$2>=2' < test.txt # print all lines where the second field is greater or equal to 2
As a beginner who’s learning about the power of unix, I am absolutely fascinated about this.
Now I am wondering if there is an easy way to perform regex substitutions only on some arbitrary fields you specify? For example, I want to do regex substitution on the third field only.
my current method is to “cut” the field I want and perform substitution on that using perl or sed, which then I “paste” to the original file. But I am wondering if there is more efficient way to achieve this.
Thanks
Since you tagged this question with ‘perl’ (in addition to ‘sed’, ‘awk’, ‘unix’, and ‘command-line’), I’ll assume you’re interested in answers that incorporate any of the above tools.
Perl has an auto-split command-line switch (
-a):…or…
-acauses an auto-split into the@Farray.-ncauses Perl to iterate over the lines of the file you feed it. The rest is programming. 😉Now for substitution:
Or, a little shorter using the
-pswitch, which tells Perl to print each line as it appears in$_. That means if you alter@F, you’ll have to copy it back into$_: