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?
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
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
Related
How do I grep within a line, and print the unmatched words?
For example, the line is something like " one two three ".
What I want is anything that is not one, and trimmed (leading and trailing spaces (could be space or tabs) removed ).
In this case, how do i get "two three"?
using sed instead:
sed -r 's/\bone\b//g;s/^\s*|\s*$//g'
E.g.
kent$ echo " one two three "|sed -r 's/\bone\b//g;s/^\s*|\s*$//g'
two three
Use sed instead:
sed -e 's/[ \t]*one[ \t]*//'
grep (GNU grep) 2.14
Hello,
I have a log file that I want to filter on a selected word. However, it tends to filter on many for example.
tail -f gateway-* | grep "P_SIP:N_iptB1T1"
This will also find words like this:
"P_SIP:N_iptB1T10"
"P_SIP:N_iptB1T11"
"P_SIP:N_iptB1T12"
etc
However, I don't want to display anything after the 1. grep is picking up 11, 12, 13, etc.
Many thanks for any suggestions,
You can restrict the word to end at 1:
tail -f gateway-* | grep "P_SIP:N_iptB1T1\>"
This will work assuming that you have a matching case which is only "P_SIP:N_iptB1T1".
But if you want to extract from P_SIP:N_iptB1T1x, and display only once, then you need to restrict to show only first match.
grep -o "P_SIP:N_iptB1T1"
-o, --only-matching show only the part of a line matching PATTERN
More info
At least two approaches can be tried:
grep -w pattern matches for full words. Seems to work for this case too, even though the pattern has punctuation.
grep pattern -m 1 to restrict the output to first match. (Also doable with grep xxx | head -1)
If the lines contains the quotes as in your example, just use the -E option in grep and match the closing quote with \". For example:
grep -E "P_SIP:N_iptB1T1\"" file
If these quotes aren't in the text file, and there's blank spaces or endlines after the word, you can match these too:
# The word is followed by one or more blanks
grep -E "P_SIP:N_iptB1T1\s+" file
# Match lines ending with the interesting word
grep -E "P_SIP:N_iptB1T1$" file
I want to grep for the string THREAD: 2. It has a space in between. Not able to figure out how.
I tried with grep "THREAD:[ \2]", but its not working
Please let me know.
Try grep "THREAD: 2" <filename>? You just want a literal '2', right?
If you are using GNU grep you could try using the alias egrep or grep -e with 'THREAD: 2$'
You might have to use '^.*THREAD: 2$'
grep reports back the entire line that has matched your pattern. If you wish to look at lines that contains THREAD: 2 then the following should work -
grep "THREAD: 2" filename
However, if you wish to fetch lines that could contain THREAD: and any number then you can use a character class. So in that case the answer would be -
grep "THREAD: [0-9]" filename
You can add + after the character class which means one or more numbers so that you can match numbers like 1,2,3 or 11,12,13 etc.
If you only want to fetch THREAD: 2 from your line then you will have to use an option of grep which is -o. It means show me only my pattern from the file not the entire line.
grep -o "THREAD: 2" filename
You can look up man page for grep and play around with all the options.
I am downlading the info.0.json file from xkcd and trying to parse just the alt text. I don't care if there are quotes around it or not. The problem it that the info.0.json file is all one line, and the alt text is in quotes after the word "alt=". Trying cat info.0.json | grep alt just returns the whole file (because it's all one line). What is the grep or sed code that will get me the alt text?
Use the -o switch:
-o, --only-matching show only the part of a line matching PATTERN
Example:
grep -o 'alt="[^"]*"'
I have lines in a file which look like the following
....... DisplayName="john" ..........
where .... represents variable number of other fields.
Using the following grep command, I am able to extract all the lines which have a valid 'DisplayName' field:
grep DisplayName="[0-9A-Za-z[:space:]]*" e:\test
However, I wish to extract just the name (ie "john") from each line instead of the whole line returned by grep. I tried piping the output into the cut command but it does not accept string delimiters.
This works for me:
awk -F "=" '/DisplayName/ {print $2}'
which returns "john". To remove the quotes for john use:
awk -F "=" '/DisplayName/ {gsub("\"","");print $2}'
Specifically:
sed 's/.*DisplayName="\(.*\)".*/\1/'
Should do, sed semantics is s/subsitutethis/forthis/ where "/" is delimiter. The escaped parentheses in combination with escaped 1 are used to keep the part of the pattern designated by parentheses. This expression keeps everything inside the parentheses after displayname and throws away the rest.
This can also work without first using grep, if you use:
sed -n 's/.*DisplayName="\(.*\)".*/\1/p'
The -n option and p flag tells sed to print just the changed lines.
More in: http://www.grymoire.com/Unix/Sed.html