Home > bash > cut a column where the delimiters are whitespaces

cut a column where the delimiters are whitespaces

Problem
You have a text where the delimiter between the columns is just one character. In this case you can use the “cut” command:

$ cat /etc/passwd | head -2
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

$ cat /etc/passwd | head -2 | cut -d: -f1
root
daemon

However, what to do if the delimiters are whitespaces (notice the plural form)? Take for instance the output of “ps ux“. How to cut the 2nd column with the PIDs?

Solution
Use the right tool. In this case, “awk” will rescue us:

$ ps ux | tail -2
jabba    28302  0.1  0.0 136216  6436 ?        S    14:20   0:09 /usr/lib/virtualbox/VBoxXPCOMIPCD
jabba    28307  0.2  0.1 785908 10568 ?        Sl   14:20   0:22 /usr/lib/virtualbox/VBoxSVC --auto-shutdown

$ ps ux | tail -2 | awk '{print $2}'
28302
28307
Categories: bash Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: