Operating System - HP-UX
1863040 Members
959 Online
110446 Solutions
New Discussion

Re: cut the specific column

 
SOLVED
Go to solution
peterchu
Super Advisor

cut the specific column

I have a file as below ,

#vi test.txt
11 22 3333
aa bb ccc

, they are separated by the space , how can I cut the third column as below.

3333
ccc
4 REPLIES 4
Dave Olker
Neighborhood Moderator
Solution

Re: cut the specific column

# cat test.txt | awk '{print $3}'


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Jim Mallett
Honored Contributor

Re: cut the specific column

I prefer Dave's method myself but just so you have a couple tools in your arsenal:

cut -d " " -f 3 test.txt

-d is the delimer
-f is the field number

Jim
Hindsight is 20/20
Hein van den Heuvel
Honored Contributor

Re: cut the specific column

Nitpicking...

I dislike one detail in Dave's solution.

Why activate an additional image (cat) and create a fresh pipe to move the data twice when awk is perfectly happy to read teh file directly?
Furthermore, the all too often (mis)used "cat | awk" reduces functionality for awk.
Notably, it can no longer 'see' the original filename, useful for good error reporting.
Not relevant for this question perhpas, but in general...

Examples:

$ cat xxx
aap noot mies teun
een twee drie vier

$ awk 'BEGIN {print FILENAME}{print $3}' x
xxx
mies
drie

$ cat x | awk 'BEGIN {print FILENAME}{print $3}'

mies
drie

fwiw,
Hein.
peterchu
Super Advisor

Re: cut the specific column

thx