2

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")

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

@Hello I seldom advertise the advantage of my solutions, but have a look at the performance overhead. 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 grep
glad it helps, as hek2mgl said awk and sed have better performances on solving such problems, and thats why i provide awk solution as well
4

awk fit's pretty good for such cases:

awk '/abc/ && !/xyz$/' input

use awk! :)

2 Comments

awk is definitely the right solution for this. but I think I might say that a lot... :-)
@EdMorton Yeah, you did! :)
2

Use two grep commands piped together. The first matches abc, the second removes the ones that end with xyz.

grep abc filename | grep -v 'xyz$'

Comments

-2
check_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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.