0

I'm using grep on text file containing some simple logs in the following form:-

[ABC.txt]
1=abc|2=def|3=ghi|4=hjk|5=lmn|6=opq
8=rst|9=uvx|10=wyz
.
.
.
.

and so on

the values for the tags 1,2,3,4 etc are different throughout the file and include special characters in some case too. Is there a way I can only retrieve the value for the tag 4 and no other tags via GREP?

BTW,this log file is itself a result of grep .So please advice if I should redirect the output first and then apply the second grep or apply the second grep over the first one,considering it's a large file.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Mayank Jha
  • 939
  • 3
  • 12
  • 24
  • 3
    please show a proper [mcve] together with expected output and what you tried already. Also, giving your initial grep would help since everything may be optimized. – fedorqui Nov 08 '16 at 12:00

2 Answers2

1
grep -Po '(?<=4=)[^|]*' ABC.txt
kay27
  • 897
  • 7
  • 16
  • 3
    While this code snippet may solve the question, including an explanation [really helps](//meta.stackexchange.com/q/114762) to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Nov 08 '16 at 13:39
  • **Flaggers / reviewers:** [For code-only answers such as this one, downvote, don't delete!](//meta.stackoverflow.com/a/260413/2747593) – Scott Weldon Nov 11 '16 at 00:15
0

You could pipe the result of grep to cut, split the fields by "|" and select the fourth field

[your grep command] | cut -d | -f 4

If you want the "4=" to be gone you can just do the same by using cut a second time but this time using "=" as a delimiter.

http://pubs.opengroup.org/onlinepubs/009695399/utilities/cut.html

eheller
  • 117
  • 1
  • 1
  • 13