1

I need to grep data from some rows and between two lines (please no awk, in this case pages are different and I want to parse them in automatic way):

Date:20.09.18
Owner:Dave
Login: 123

data-begin
 some text
 some text
 some text
 some text
 some text
data-end

using grep -o -a -E 'Date|Owner|Login' I can grep 3 first lines, but how can I grep data between two lines in Pipe?

Tried this, but no result...

grep -o -a -E 'Date|Owner|Login' | grep -A6 data-begin |

Edited:

Date:20.09.18
Owner:Dave
Login: 123

some useless text that is not needed

data-begin
 some text
 some text
 some text
 some text
 some text
data-end

How to grab text without text in middle?

Jennifer
  • 19
  • 3
  • Welcome to SO. With the `|` you are applying grep on the output of `grep -o -a -E 'Date|Owner|Login'`, which doesn't contain `data-begin`, right? – Daemon Painter Sep 05 '19 at 07:05
  • I think perhaps grep might be too simplistic. If it's yaml you may hive yq a chance ( a jq clone) – Alexander Oh Sep 05 '19 at 07:10
  • Yes, you're right, data-begin should be written in first grep, but it greps only line in this way not range – Jennifer Sep 05 '19 at 07:10
  • Otherwise https://stackoverflow.com/q/2686147/887836 – Alexander Oh Sep 05 '19 at 07:12
  • Your command [yields an error](https://ideone.com/b2QFho). What exactly are you trying to achieve? get as a result? – Wiktor Stribiżew Sep 05 '19 at 07:25
  • Output should be like this: |20.09.18|Dave|123|data between begin and end| . Parse and add "|" is easy, problem is in grabbing data in Pipe – Jennifer Sep 05 '19 at 08:49
  • Your statement of _Please no awk_ is a bit hard. With grep you will not manage this task unless you use GNU grep with perl expressions. You mention no awk, but you would accept sed. I understand that awk might be daunting sometimes, but when you start to get a feel for it ... you'll realize it is often the quickest way to get anything done with a text file. This might be useful too: https://unix.stackexchange.com/questions/99431/print-lines-between-and-including-two-patterns – kvantour Sep 05 '19 at 09:11
  • Yes, awk is good, but this is html page (yes, parsing html page with bash is masochism, and not my desicion.). I'm ready to learn awk, if it is possible to get from text what I need in Pipe. I'm awking output after grep and sed. – Jennifer Sep 05 '19 at 09:30

2 Answers2

0

Could you please try following if you are ok with sed.

sed -E -n '/Date|Owner|Login/p;/data-begin/,+6p' Input_file

OR in case you want to only get values before : what I could see from your attempt then try following sed.

sed -E -n '/Date|Owner|Login/{s/:.*//p};/data-begin/,+6p'  Input_file

Output will be as follows.

Date
Owner
Login
data-begin
 some text
 some text
 some text
 some text
 some text
data-end


Now part comes why your attempt was not working, because you are sending your 1st grep's output to 2nd grep where it is not finding any string like data-begin hence nothing is getting printed by it.


EDIT: As per OP's comment, output needed is changed so adding this one now.

sed -E -n '/Date|Owner|Login/{s/.*://p};/data-begin/,+6p'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Yes, thanks, It answers question. But is it possible to not to grab data between Login and data-begin? This sed grabs text between lines – Jennifer Sep 05 '19 at 08:46
  • @Jennifer, I don't think so it is taking it, please see my EDIT in my post where I have posted now what output I am getting now too, let me know. – RavinderSingh13 Sep 05 '19 at 08:52
  • Yes, It should be like it, but instead of Data|Owner|Login I tried Date.*data-begin/p;/data-begin/,+6p' and parsed it – Jennifer Sep 05 '19 at 08:55
  • @Jennifer, ok try `sed -E -n '/Date|Owner|Login/{s/.*://p};/data-begin/,+6p' Input_file` once and let me know? – RavinderSingh13 Sep 05 '19 at 08:57
  • After last command I'm getting whole text from file from begging to 6'th row after data-begin – Jennifer Sep 05 '19 at 09:03
  • @Jennifer, No that is not the case with me, can you please check if you have control M characters by doing `cat -v Input_file` in your file and let me know? – RavinderSingh13 Sep 05 '19 at 09:04
0

If I understand correctly, you want to discard some unwanted stuff after "Login:" and before "data-begin", in a pipe (?) only using grep. If so, this seems to fit the bill:

{ grep -B999 "^Login:" file; grep -A999 "^data-begin" file; } | next_command

Output

Date:20.09.18
Owner:Dave
Login: 123
data-begin
 some text
 some text
 some text
 some text
 some text
data-end
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432