-1

I need to grep all characters between second and third | (pipe) character from a file.

Let's say we have a file with string like below (two lines):

abc123 | def123 | ghi123 | jkl123 | mno123
abc123 | def123 | jkl123 | ghi123 | mno123

After I use grep/sed/awk command I should get like

ghi123
jkl123 

I would appreciate any clue or help.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
odincer
  • 329
  • 6
  • 16

1 Answers1

3

If you want always to get third element, you can try with:

echo "abc123 | def123 | ghi123 | jkl123 | mno123" | awk -F " | " '{print $5}'

Or:

echo "abc123 | def123 | ghi123 | jkl123 | mno123" | cut -d '|' -f 3 | tr -d ' '

Output:

ghi123

For a string with many words between | you can use:

echo "abc123 | def123 | foo bar | jkl123 buz | mno123" | cut -d '|' -f 3 | sed -e 's/^ //' | sed -e 's/ $//'

Output:

foo bar

Note that sed -e 's/^ //' | sed -e 's/ $//' is used for removeng first and last whitespace, because tr -d ' ' removes all whitespaces from the string.

hsz
  • 148,279
  • 62
  • 259
  • 315
  • Well it is not always one word,strings could be like below abc1323 abc123 | bdc123 bdc123 bdc123 | jkl123 jk ... So i need all strings between second and third pipe – odincer May 29 '14 at 13:03
  • 1
    wouldnt it be `{print $3}` ? –  May 29 '14 at 13:06
  • @Jidder Unfortunately not, because `|` are also counted – hsz May 29 '14 at 13:07
  • `awk -F'|' '{gsub(" ",""); print $3}' file` why does this work with $3 without the spaces ? I know what the command does as i wrote it. But i'm interested in why this doesnt count the | ? –  May 29 '14 at 13:10
  • @hek2mgl Is that why its `$5` because its actually delimiting on spaces and | ? –  May 29 '14 at 13:18
  • @Jidder I thought I were sure about this but currently don't think I'm sure anymore. That's why I deleted the comment. I'm still trying to find it out :) – hek2mgl May 29 '14 at 13:20
  • @hek2mgl Not sure if you found the answer yet. Its because awk is recognising `|` as an `or` sign. Putting it within square brackets seems to solve the problem. Multiple characters find the string but regex needs making literal. Backslashing it also doesnt seem to work. –  May 29 '14 at 13:28
  • Double backslashing the pipe seems to work. It appears that it is parsed twice :) –  May 29 '14 at 13:30
  • 1
    @Jidder YEAH! :) You made it! please post it as an answer :) Now, after reading the `awk` man page sections of `FIELDS` and `REGULAR EXPRESSIONS` it is clear what is going on. `-F' | '` will select only the space as delimiter, or better spoken `space or space`. This is because a multi-character delimiter will be threatened as a regular expression. – hek2mgl May 29 '14 at 13:31