Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I don't think you can achieve that in a one-step process. While grep with -E
happily supports extended regular expressions you can't have the -v feature
of grep for a non-match combined w/ a match.
You'll be looking at running your grep, and then suppressing things that
match comments in a 2nd grep.
Your idea is not working since the result of the below command contains path name of the file, so every row is getting excluded since the path name contians '/'
My understanding of this problem is that you wish exclude lines when any one of a number of patterns is matched on a line. I'm assuming that the part involving IP patterns is solved. A bit of awk can address a situation where a matching any of a number of strings must be excluded. For example, in the script below, the results are from section 3 (after the context and data file are displayed). The first 2 sections detail how the script was run and what the data looked like; those contain some non-standard elements, but the solution is standard:
Code:
#!/usr/bin/env bash
# @(#) s1 Demonstrate line exclusion with awk.
# Section 1, setup, pre-solution.
# Infrastructure details, environment, commands for forum posts.
# Uncomment export command to test script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
C=$HOME/bin/context && [ -f $C ] && . $C specimen awk
set -o nounset
pe
FILE=${1-data1}
# Section 2, display input file.
# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen 7 $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"
# Section 3, solution.
pl " Results:"
awk '
!/[#]|[*]|[/]|apple/
' $FILE
exit 0
producing:
Code:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0 (lenny)
GNU bash 3.2.39
specimen (local) 1.17
GNU Awk 3.1.5
|| start [ first:middle:last ]
Whole: 7:0:7 of 13 lines in file "data1"
Line 1
# at the beginning
* at the beginning
/ at the beginning
A line with # in the middle
A line with * in the middle
A line with / in the middle
Line someplace in the file
We like apples
Finally, symbol at the end #
Finally, symbol at the end *
Finally, symbol at the end /
Last line
|| end
-----
Results:
Line 1
Line someplace in the file
Last line
The awk script attempts to match any of the alternatives separated by "|". The "[]" symbols serve to effectively quote the enclosed characters, often a help in reading the patterns. The leading "!" is to invert the sense of the match so that if any pattern matches, the implicit print action is avoided. Only when none of the patterns match and the sense is inverted (to true), will the line be printed.
I prefer single passes over data files if it can be done easily.
Best wishes ... cheers, makyo
( Edit 1 )
I just noticed the issue with "/" in the pathname. Do you need the pathname for this problem? If not, then changing the "H" to "h" will eliminate the pathname. If you do need it, then another step will be needed.
You need to escape the asterisk, or put the glob pattern in quotes. Otherwise it will expand to a list of all log files before the command is executed:
--exclude=filename1.log filename2.log ...l
That section is intended to display the input file, and is supposed to execute head-tail commands if specimen is not available. Perhaps cygwin behaves differently, so replace the lines:
Code:
specimen 7 $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"
with:
Code:
cat $FILE
Re-run the code, and you should get the results I did ... cheers, makyo
The first part of that script uses some local code to display the computing environment in which that code was run. Let us omit that for now. Please copy and run this:
Code:
#!/usr/bin/env bash
# @(#) s5 Demonstrate line exclusion with awk, minimal version.
FILE=${1-data1}
# Section 2, display input file.
# Display sample of data file, with head & tail as a last resort.
echo
echo " Data file $FILE:"
cat $FILE
# Section 3, solution.
echo
echo " Results:"
awk '
!/[#]|[*]|[/]|apple/
' $FILE
exit 0
which produces this on my system:
Code:
% ./s5
Data file data1:
Line 1
# at the beginning
* at the beginning
/ at the beginning
A line with # in the middle
A line with * in the middle
A line with / in the middle
Line someplace in the file
We like apples
Finally, symbol at the end #
Finally, symbol at the end *
Finally, symbol at the end /
Last line
Results:
Line 1
Line someplace in the file
Last line
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.