Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Example:

3|100|test@test.com|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|

Expected view after grep:

test@test.com
share|improve this question

migrated from stackoverflow.com Oct 11 '12 at 14:34

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

1  
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
add comment

5 Answers

up vote 7 down vote accepted

Use cut:

$ echo '3|100|test@test.com|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
test@test.com
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. wiki.debian.org/TheUnixWay faqs.org/docs/artu/ch01s06.html –  user95605 Oct 11 '12 at 12:04
add comment

Why grep? Use cut

echo "3|100|test@test.com|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
add comment

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
1  
Extracting multiple fields is possible with cut: cut -d'|' -f3-5 –  user95605 Oct 11 '12 at 12:10
2  
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
2  
+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
add comment

Just Imagine your content is present under this file file1

[max@localhost ~]$ cat file1 3|100|test@test.com|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

test@test.com

Here

-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

root:x:0:0:root:/root:/bin/bash

Then we have to use : as delimiter like this

To cut first field use f1

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

To cut second field use f2

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

To cut third field use f3

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

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
add comment

Your Answer

 
discard

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.