Combining grep flags - grep

I'm trying to find lines that start with 'query' or the following sign: '>' but I don't know how to do this.
If this is the dataset:
query=345
query=4565
brink=980
>ehlhdhdk
>blonk
I want to only preserve the lines 1,2,4 and 5.
I have tried: grep -e 'query=' filename.txt||grep -F '>' filename.txt > newfile.txt
and:
cat filename.txt | grep -e 'query='||grep -F '>' > newfile.txt.
But these do not work and they do not output the newfile.txt and instead they just output into the terminal.

You can use
grep -E '^(query|>)' filename.txt > newfile.txt
Details
^ - start of string
(query|>) - a capturing group that matches either
query - query
| - or
> - a > char.
See the online demo:
#!/bin/bash
s='query=345
query=4565
brink=980
>ehlhdhdk
>blonk'
grep -E '^(query|>)' <<< "$s"
Output:
query=345
query=4565
>ehlhdhdk
>blonk

Use multiple patterns with option -e:
grep -e '^>' -e '^query' file
Output:
query=345
query=4565
>ehlhdhdk
>blonk

Related

Grep find pattern but print another line

I want to match all cyrillic characters, but print the ID to file. For example:
Author: Doe, John
Title: Оптимизация ресурсного потенциала промышленности города с учетом его конкурентных преимуществ
ID: 1234567
My current approach is to grep for cyrillic characters:
grep -i -r --include=*{rdf,redif,rdf~} --color="auto" -P -n '[\x{0400}-\x{04FF}]' > cyrillic.txt
How can I just print the ID line to a file and not the matching line?
Use -A1 option if the ID: line is right after the matching pattern. Then pipe it to another grep to get the line with ID:.
grep -A1 -i -r --include=*{rdf,redif,rdf~} --color="auto" -P -n '[\x{0400}-\x{04FF}]' \
| grep 'ID: ' > cryllic.txt
Use grep flag h - to suppress output file names - you'll have output like:
4:string with matching pattern
5:string with matching pattern
7:string with matching pattern
Now you can pipe this output into awk and print only fist column, which is matching string number:
{your_grep} | awk -F ':' '{print $1}' > cyrillic.txt

grep for variable pattern: echo "${foo}" | grep "'${bar}'"

I will need to grep one variable for a variable pattern.
Like so
foo="--test2"
bar="--test"
echo "${foo}" | grep "'${bar}'"
Unfortunately it is not working.
Any ideas about how to achieve this?
If you use , single quotes it will take it as literal string. Remove the single quotes. Then it will throw the error for -- in your string. For that use -e option for mention that is a pattern to match.
echo "${foo}" | grep -e "${bar}"
your pattern is leading with -, -e option is needed.
this line should work for your example:
echo "${foo}" | grep -e "${bar}"

trying to grep '--string' fails

I'm trying to grep for a string that starts with "--"
for some reason it counted as special character, but even when trying to use -F then grep gives me bad syntax:
[root#pc-01 /]# grep -F --restore .
-bash: --restore: command not found
any tips?
Thanks.
Try following.
grep -F -- --restore filename
You can escape the first - :
Without escaping:
[root#TIAGO-TEST2 tmp]# echo '--aa --bb --cc' | grep -o '--b'
grep: option '--b' is ambiguous; possibilities: '--basic-regexp' '--binary' '--byte-offset' '--binary-files' '--before-context'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Escaping:
[root#TIAGO-TEST2 tmp]# echo '--aa --bb --cc' | grep -o '\--b'
--b

Grep digits after match

I would like to grep digits inside a set of parentheses after a match.
Given foo.txt below,
foo: "32.1" bar: "42.0" misc: "52.3"
I want to extract the number after bar, 42.0.
The following line will match, but I'd like to extract the digit. I guess I could pipe the output back into grep looking for \d+.\d+, but is there a better way?
grep -o -P 'bar: "\d+.\d+"' foo.txt
One way is to use look ahead and look-behind assertions:
grep -o -P '(?<=bar: ")\d+.\d+(?=")'
Another is to use sed:
sed -e 's/.*bar: "\([[:digit:]]\+.[[:digit:]]\+\)".*/\1/'
You could use the below grep also,
$ echo 'foo: "32.1" bar: "42.0" misc: "52.3"' | grep -oP 'bar:\s+"\K[^"]*(?=")'
42.0

remove multiple lines from text file using grep

I would like to remove unwanted lines from my text file had certain words. i have use grep -v like this
grep -v 'error|fault|unkownn' input.txt > out.txt
it's working with one word but not on multiple words. did i miss anything?
| is only treated as a a regex character when grep is working in extended regex mode. So you need to do one of the following:
# Escape the | so that it's treated as a regex control character
grep -v 'error\|fault\|unkownn' input.txt > out.txt
# -E enables extended regex mode
grep -vE 'error|fault|unkownn' input.txt > out.txt
# egrep = grep -E
egrep -v 'error|fault|unkownn' input.txt > out.txt

Resources