avi
avi

Reputation: 21

Output only specific lines in unix

I'm trying to remove lines from the output that contains "xyz" in 1st column using awk as

grep -H --with-filename "Rule" *| awk '$1!=" \*xyz\* "{print $0}'

but I'm not having any success with this.

For example after doing grep -H --with-filename "Rule" I'm getting the output as

file_xyz.log abc p12r
file1.log asd ef23t
fi_xyz.log gbc or26r
file1.log asd ef2t

but I want to remove all lines which contain xyz.

Upvotes: 1

Views: 110

Answers (2)

Ed Morton
Ed Morton

Reputation: 203209

Some notes on your question:

  1. What you have isn't very close to valid awk syntax, you should find an intro to awk tutorial or read a few pages of the manual to get started. I highly recommend everyone get the book Effective Awk Programming, 4th Edition, by Arnold Robbins.
  2. A glance at the grep man page will tell you that -H and --with-filename are the short and long versions of exactly the same option - you don't need to use both, just one of them.
  3. The string Rule doesn't appear anywhere in the output you say you get when grep-ing for Rule and grep -H will output a : after the file name while you show a blank - make sure your input, output, and code are consistent and correct when asking a question.
  4. The approach you're trying to use will fail for filenames that contain spaces.
  5. You never need grep when you're using awk.

This is probably all you need:

awk '(FILENAME !~ /xyz/) && /Rule/{print FILENAME, $0}' *

but there are also ways in some shells (see https://unix.stackexchange.com/q/164025/133219 and https://unix.stackexchange.com/q/335484/133219 for bash examples) to specify a globbing pattern that excludes some strings so then you never open them to search inside in the first place.

Upvotes: 1

Digvijay S
Digvijay S

Reputation: 2705

try

grep -H --with-filename "Rule" *| awk '$1 !~ /xyz/'

Upvotes: 0

Related Questions