Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top



Expected view after grep:
share|improve this question

migrated from Oct 11 '12 at 14:34

This question came from our site for professional and enthusiast programmers.

Welcome to Stack Overflow! We encourage you to research your questions. If you've tried something already, please add it to the question - if not, research and attempt your question first, and then come back. – user95605 Oct 11 '12 at 12:00
up vote 7 down vote accepted

Use cut:

$ echo '3|100||0|0|6:1,10,11,12,13,2,3,4,5,6,9|7:1,10,11,13,16,2,4,5,6,9|' |\
cut -d'|' -f3
share|improve this answer
simple and perfect thank you. – Kalin Borisov Oct 11 '12 at 12:01
That's the Un*x spirit: small tools that do one task well. – user95605 Oct 11 '12 at 12:04

Maybe awk is better suited for this usage:

awk 'BEGIN { FS = "|" } ; { print $3 }'

If you have to extract more than one field from such an input, I think it is the easiest using awk.

(OFF: excuse me if I pointed in an awk-ward direction)

share|improve this answer
Maybe? Why do you think so? – user95605 Oct 11 '12 at 12:06
Extracting multiple fields is possible with cut: cut -d'|' -f3-5 – user95605 Oct 11 '12 at 12:10
From my previous experiences, I assumed that either the poster, or someone else finding this page will face the issue that more than one column is required from the input. But a less complicated tool (cut) might be faster than universal ones (awk), if the restricted feature set of the lighter tool is adequate. (I don't have data to back this) Also, for me, the awk command is more readable. If I ever had to re-read the script written, I'd like to see the awk line in favor of the others - but that is just a personal opinion, not an objective fact. That's why only 'maybe'. – ppeterka Oct 11 '12 at 12:18
+1 Sorry, I had not done my homework on cut, thanks for pointing that out! – ppeterka Oct 11 '12 at 12:19
OK, thanks for explaining your motivation. – user95605 Oct 11 '12 at 12:23

Why grep? Use cut

echo "3|100||0|0|6:1,10,11,12,13,2,3,4,5,6,9|7:1,10,11,13,16,2,4,5,6,9|" | cut -d '|' -f 3
share|improve this answer

Just Imagine your content is present under this file file1

[max@localhost ~]$ cat file1 3|100||0|0|6:1,10,11,12,13,2,3,4,5,6,9|7:1,10,11,13,16,2,4,5,6,9|

To cut the third field use this command

[max@localhost ~]$ cut -d "|" -f3 file1


-d : Specifies to use character | as delimiter

-f1 : Print first field, if you want print second field use -f2, third field use -f3, and so on...

suppose file1 content is like this

[max@localhost ~]$ cat file1


Then we have to use : as delimiter like this

To cut first field use f1

[max@localhost ~]$ cut -d ":" -f1 file1

To cut second field use f2

[max@localhost ~]$ cut -d ":" -f2 file1

To cut third field use f3

[max@localhost ~]$ cut -d ":" -f3 file1
share|improve this answer
Thank you, Max fro your additional comment and details. – Kalin Borisov Oct 15 '12 at 7:06

Just for fun, here's how you could do it with grep and tr:

<infile grep -Eo '^([^|]+\|){3}' | grep -Eo '[^|]+\|$' | tr -d '|'

The first regex grabs the first three pipe delimited fields. The second grep picks out the last field and tr removes the remaining delimiter.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.