Home > Software design >  Extract part of text using bash on a large file
Extract part of text using bash on a large file

Time:03-03

I have huge log file where I want to extract part of the log which looks like this:
----TEXT NOT NEEDED--- yValues:10 zValues:1254 ----TEXT NOT NEEDED---
----TEXT NOT NEEDED--- yValues:10 zValues:1254 ----TEXT NOT NEEDED---
----TEXT NOT NEEDED--- yValues:10 zValues:1254 ----TEXT NOT NEEDED---
....
This is a huge file and this yValues and zValues are repeated throughout.
Opening the file is very slow, so I am trying to extract this part of the text only.
If I do grep yValues, it still gives the whole line and so file saved is still a large file.
How can this be done using bash?

CodePudding user response:

grep has a -o/--only-matching option to do what you want:

$ grep --only-matching 'yValues:[0-9]\ ' <<EOF
----TEXT NOT NEEDED--- yValues:10 zValues:1254 ----TEXT NOT NEEDED---
----TEXT NOT NEEDED--- yValues:10 zValues:1254 ----TEXT NOT NEEDED---
----TEXT NOT NEEDED--- yValues:10 zValues:1254 ----TEXT NOT NEEDED---
EOF
yValues:10
yValues:10
yValues:10
  •  Tags:  
  • bash
  • Related