Skip to content

Instantly share code, notes, and snippets.

@tbnorth
Last active November 16, 2020 01:49
Show Gist options
  • Save tbnorth/4927939513fff0e6ce1f8f1b7b0bf7b9 to your computer and use it in GitHub Desktop.
Save tbnorth/4927939513fff0e6ce1f8f1b7b0bf7b9 to your computer and use it in GitHub Desktop.
sed select column

A sed solution that selects a column by number:

echo 0 1 2 3 iv 5 6 | sed -E 's/^(\S+\s+){4}(\S+).*/\2/'

or

echo 0 1 2 3 iv 5 6 | sed 's/^\(\S\+\s\+\)\{4\}\(\S\+\).*/\2/'

replace the 4 (not the \2 at the end) with the column number you want, starting from zero. Column delimiters are any combination of any number of tabs and spaces. -r flag uses extended regex syntax which is more concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment