How to select not commented rows? - linux

I have file that contains:
# sdfdsfds fsf
var1=1232
#fdsfdsfds
#fdsfsdf
var2=456
..................
I need select only not commented rows - that not start from #
Does it possible with grep?
Thanks.

use the -v(invert-match) option in grep:
$ grep -v '^#' file.txt
var1=1232
var2=456

The following will do it:
grep -v ^# file.txt

You may use grep -v '^#'
The -v option is for not logic.

Use the -E option for regex and -v option for inverse matching.
grep -v -E '^#' file

Related

Display the lines of file that contain the pattern 'Pap' but they don't contain the pattern 'Aig'

How to make such a double query ?
I try grep -e Pap -e -v Aig filename.txt and grep 'Pap' && -v 'Aig' filename.txt
but both don't work
Use a pipe between your filters
grep -e Pap filename.txt | grep -v Aig

Why grep gives error for search pattern i.e. grep: <some pattern>: No such file or directory

I want to print the selected lines using grep pattern matching. I am using following command -
cat MyTest.txt | grep -v -E B1 "EEB|SET|PET"
grep: EEB|SET|PET: No such file or directory
I am always getting above grep error.
I want to print the line which matches pattern or patterns I have mentioned i.e EEB or SET or PET or All of these and
A single line prior to matching line. hence option -B1
You can use this command without useless cat:
grep -v -E -B1 "EEB|SET|PET" MyTest.txt
Note - before B1.
However from your description it appears you may not need -v (inverse results) and want this:
grep -E -B1 "EEB|SET|PET" MyTest.txt
Grep has the following syntax:
grep options pattern input_file_names
where options are optional, but not pattern. So B1 without "-" is used as pattern and "EEB|SET|PET" as file_names.
You should change "B1" to "-B1".
As recommendation
cat MyTest.txt | grep -v -E -B1 "EEB|SET|PET"
to
grep -v -E -B1 "EEB|SET|PET" MyTest.txt

grep exclude multiple strings

I am trying to see a log file using tail -f and want to exclude all lines containing the following strings:
Nopaging the limit is and keyword to remove is
I am able to exclude one string like this:
tail -f admin.log|grep -v "Nopaging the limit is"
But how do I exclude lines containing either of string1 or string2?
Filtering out multiple lines with grep:
Put these lines in filename.txt to test:
abc
def
ghi
jkl
grep command using -E flag with a pipe between tokens in a string:
grep -Ev 'def|jkl' filename.txt
prints:
abc
ghi
egrep using -v flag with pipe between tokens surrounded by parens:
egrep -v '(def|jkl)' filename.txt
prints:
abc
ghi
Or if stacking -e flags through grep parameters is okay (credit -> #Frizlab):
grep -Fv -e def -e jkl filename.txt
prints:
abc
ghi
grep -Fv -e 'Nopaging the limit is' -e 'keyword to remove is'
-F matches by literal strings (instead of regex)
-v inverts the match
-e allows for multiple search patterns (all literal and inverted)
Another option is to create a exclude list, this is particulary usefull when you have a long list of things to exclude.
vi /root/scripts/exclude_list.txt
Now add what you would like to exclude
Nopaging the limit is
keyword to remove is
Now use grep to remove lines from your file log file and view information not excluded.
grep -v -f /root/scripts/exclude_list.txt /var/log/admin.log
egrep -v "Nopaging the limit is|keyword to remove is"
tail -f admin.log|grep -v -E '(Nopaging the limit is|keyword to remove is)'
You can use regular grep like this:
tail -f admin.log | grep -v "Nopaging the limit is\|keyword to remove is"
The greps can be chained. For example:
tail -f admin.log | grep -v "Nopaging the limit is" | grep -v "keyword to remove is"
If you want to use regex:
grep -Ev -e "^1" -e '^lt' -e 'John'

Linux, Print all lines in a file, NOT starting with

I would like to print the contents of a file, but all lines starting with # I want to ignore. I was trying some stuff with grep and awk, but it kept printing the whole file, or just printed the lines starting with #. I you could give me a push in the right way, or a grep/awk command that would print anyline in the file that does not start with #.
Use the -v option of grep to negate the condition:
grep -v '^#' file
You can use the ! operator:
awk '!/^ *#/ { print; }'
This negates the result of the match. I also included lines that start with spaces and then #, but you can tailor the regex how you like.
You could use grep to exclude all lines that begin with # using the -v option
grep -v '^#' filename
If you're a fan of sed:
sed '/^#/d' filename
This would also leave out lines with whitespace before the # :
awk '$1!~/^#/' file
or
grep -v '^[[:blank:]]*#' file
Here is the grep PCRE way,
grep -P '^(?!#)' file

Remove blank lines with grep

I tried grep -v '^$' in Linux and that didn't work. This file came from a Windows file system.
Try the following:
grep -v -e '^$' foo.txt
The -e option allows regex patterns for matching.
The single quotes around ^$ makes it work for Cshell. Other shells will be happy with either single or double quotes.
UPDATE: This works for me for a file with blank lines or "all white space" (such as windows lines with \r\n style line endings), whereas the above only removes files with blank lines and unix style line endings:
grep -v -e '^[[:space:]]*$' foo.txt
Keep it simple.
grep . filename.txt
Use:
$ dos2unix file
$ grep -v "^$" file
Or just simply awk:
awk 'NF' file
If you don't have dos2unix, then you can use tools like tr:
tr -d '\r' < "$file" > t ; mv t "$file"
grep -v "^[[:space:]]*$"
The -v makes it print lines that do not completely match
===Each part explained===
^ match start of line
[[:space:]] match whitespace- spaces, tabs, carriage returns, etc.
* previous match (whitespace) may exist from 0 to infinite times
$ match end of line
Running the code-
$ echo "
> hello
>
> ok" |
> grep -v "^[[:space:]]*$"
hello
ok
To understand more about how/why this works, I recommend reading up on regular expressions. http://www.regular-expressions.info/tutorial.html
If you have sequences of multiple blank lines in a row, and would like only one blank line per sequence, try
grep -v "unwantedThing" foo.txt | cat -s
cat -s suppresses repeated empty output lines.
Your output would go from
match1
match2
to
match1
match2
The three blank lines in the original output would be compressed or "squeezed" into one blank line.
The same as the previous answers:
grep -v -e '^$' foo.txt
Here, grep -e means the extended version of grep. '^$' means that there isn't any character between ^(Start of line) and $(end of line). '^' and '$' are regex characters.
So the command grep -v will print all the lines that do not match this pattern (No characters between ^ and $).
This way, empty blank lines are eliminated.
I prefer using egrep, though in my test with a genuine file with blank line your approach worked fine (though without quotation marks in my test). This worked too:
egrep -v "^(\r?\n)?$" filename.txt
Do lines in the file have whitespace characters?
If so then
grep "\S" file.txt
Otherwise
grep . file.txt
Answer obtained from:
https://serverfault.com/a/688789
This code removes blank lines and lines that start with "#"
grep -v "^#" file.txt | grep -v ^[[:space:]]*$
awk 'NF' file-with-blank-lines > file-with-no-blank-lines
It's true that the use of grep -v -e '^$' can work, however it does not remove blank lines that have 1 or more spaces in them. I found the easiest and simplest answer for removing blank lines is the use of awk. The following is a modified a bit from the awk guys above:
awk 'NF' foo.txt
But since this question is for using grep I'm going to answer the following:
grep -v '^ *$' foo.txt
Note: the blank space between the ^ and *.
Or you can use the \s to represent blank space like this:
grep -v '^\s*$' foo.txt
I tried hard, but this seems to work (assuming \r is biting you here):
printf "\r" | egrep -xv "[[:space:]]*"
Using Perl:
perl -ne 'print if /\S/'
\S means match non-blank characters.
egrep -v "^\s\s+"
egrep already do regex, and the \s is white space.
The + duplicates current pattern.
The ^ is for the start
Use:
grep pattern filename.txt | uniq
Here is another way of removing the white lines and lines starting with the # sign. I think this is quite useful to read configuration files.
[root#localhost ~]# cat /etc/sudoers | egrep -v '^(#|$)'
Defaults requiretty
Defaults !visiblepw
Defaults always_set_home
Defaults env_reset
Defaults env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR
LS_COLORS"
root ALL=(ALL) ALL
%wheel ALL=(ALL) ALL
stack ALL=(ALL) NOPASSWD: ALL
Read lines from file exclude EMPTY Lines
grep -v '^$' folderlist.txt
folderlist.txt
folder1/test
folder2
folder3
folder4/backup
folder5/backup
Results will be:
folder1/test
folder2
folder3
folder4/backup
folder5/backup

Resources