Can I use grep command to look for all the lines in a file that have"abc" in them, but exclude the lines that end in say "xyz"?
Eg grep 'abc' fileName (some way to exclude all lines ending in "xyz")
Try this:
hzhang@dell-work ~ $ cat sample.csv
abc, xyz
abc,1
abc,2
abc,3,xyz
hzhang@dell-work ~ $ grep abc sample.csv |grep -v "xyz$"
abc,1
abc,2
The explanation of -v:
-v, --invert-match
Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)
If you can use awk, just check the patterns:
hzhang@dell-work ~ $ awk '/abc/ && !/xyz$/' sample.csv
abc,1
abc,2
sed and awk solutions need to iterate over the file just once while the above grep solution is doing it twice. What if you have 3 or more conditions? Would you iterate over the file so much times? awk '/abc/&&!/xyz$/' is the way to go. You need to get fluent with such simple awk invocations the same as with grepcheck_passwd() {
if [ ! -f ./passwd_copy.txt ]; then
echo "brak pliku passwd_copy.txt, aby utworzyc uruchom skrypt z poleceniem -INIT"
exit 1
fi
}
case $1 in
"-AUTHOR")
echo "Autor skryptu: $autor"
echo "Grupa szkoleniowa $grupa"
echo "Nazwa skryptu: $0"
;;
"-INIT")
if [ $(cp /etc/passwd ./passwd_copy.txt) ] ; then
echo "kopia nie ok"
else
echo "kopia ok"
fi
;;
"-FIND")
check_passwd
if [ -z "$2" ]; then
echo "brak ciagu do wyszukania"
exit 1
fi
grep "$2" passwd_copy.txt > wynik_$(date +%d-%m-%Y).txt
;;
"-COLUMNS")
check_passwd
if [ -z "$2" ]; then
echo "Brak numeru kolumny"
exit 1
fi
awk -v col="$2" -F: '{print $col}' passwd_copy.txt 1>$2
;;
"-RIGHTS")
check_passwd
if [ -z "$2" ] || [ -z "$3" ]; then
echo "brak katalogu lub praw"
exit 1
fi
for file in "$2"/*; do
if [ -f "$file" ]; then
chmod "$3" "$file"
echo "zmieniono prawa dla pliku $file"
fi
done
;;
*)
echo "Możliwe do wpisania parametry to -AUTHOR -INIT -FIND -COLUMNS -RIGHTS"
;;
esac