You can do this using grep
's -f
option (that's lower-case -f
, not -F
):
% echo -e 'Dog\nFish\nCat\nShoes' > ./file1.txt
% echo -e '1,shoes,red\n2,apple,black\n3,fog,blue' > ./file2.csv
# Grab all lines from the CSV that match a pattern from file1:
% grep -if ./file1.txt ./file2.csv
1,shoes,red
# Grab all lines from the CSV that DON'T match a pattern from file1:
% grep -vif ./file1.txt ./file2.csv
2,apple,black
3,fog,blue
Detailed explanation:
grep
— self-explanatory
-v
— means 'return lines not matching the input pattern'
-i
— means 'use case-insensitive matching' (since your first file had capital letters and the CSV didn't)
-f
— means 'interpret each line in the specified file (file1.txt
) as a pattern to use for matching'
Depending on the results you want and the contents of your files, you may also want to read into the -F
and -w
options.
If you need to edit the file in-place, i think you can do this with sed
's -f
option, but sed
interprets each line of the file as a command rather than a simple pattern like grep
does.
It might be easier if you use more than one line to do this. You could write a bash script:
#!/bin/bash
tail -f test.log | while read line; do
if echo "$line" | grep -q "Error"; then
echo "$line" >> error.log
elif echo "$line" | grep -q "Warning"; then
echo "$line" >> warning.log
# The following is in case you want to print out lines that do not match
else
echo "$line"
fi
done
Best Answer
You can use this command:
wc -l <file>
This will return the total line number count in the provided file.