Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to delete the entire line for certain keywords

Status
Not open for further replies.

jr8rdt

IS-IT--Management
Feb 9, 2006
59
US
I have a file which I need to trim. I want to delete entire lines which has the word "ABC". there are several lines which has that words. how to do that using awk. I know how to do it for column but I don't know how to do it with line.
 
can't do it
/ABC/: event not found
 
It works now.
the bang (!) should be outside the quote not inside.

Thanks.
 
What OS are you on?

works for plain old awk on AIX...

perhaps you should try:

awk ' ! /ABC/ ' /path/to/your/file

or

awk " ! /ABC/ " \path\to\your\file



HTH,

p5wizard
 
Hi

p5wizard, you probably use [tt]ksh[/tt] and jr8rdt probably use [tt]bash[/tt]. In [tt]bash[/tt] if the first unquoted character of a string is an exclamation mark ( ! ), is considered an event designator.

Feherke.
 
What are event designators and how are they used, Feherke? (Yes, yes, I know, RTFM...)

Annihilannic.
 
Hi

No exact idea, Annihilannic. :) I never used them, I just hit the same problem as jr8rdt. They are kind of searches in the history.
Code:
[blue]master #[/blue] date -u
Fri Feb 10 16:47:20 UTC 2006
[blue]master #[/blue] cal 2006 | less
[blue]master #[/blue] [gray]# other command, with name [b]not[/b] begining with "d"[/gray]
[blue]master #[/blue] if !d | grep -q Fri; then echo "weekend approaching"; fi
weekend approaching
After the above commands if you press the up-arrow key to go back is the [tt]bash[/tt] history, you will see that the [tt]if[/tt] command was rewritten before execution, the "[tt]!d[/tt]" being substituted with [tt]date -u[/tt].

I would say that their name, "Event Designator" sounds better then what they can do.

Feherke.
 
Sounds like C-shell history functions (perhaps the most evil shell on the planet). "!!" is probably replaced with the previous command too? I know I could try myself, but it's fun watching you do it. :)

Annihilannic.
 
Annihilannic
man_bash said:
Event Designators
An event designator is a reference to a command line entry in the history list.

! Start a history substitution, except when followed by a blank, newline, carriage return, = or ( (when the extglob
shell option is enabled using the shopt builtin).
!n Refer to command line n.
!-n Refer to the current command line minus n.
!! Refer to the previous command. This is a synonym for `!-1'.
!string
Refer to the most recent command starting with string.
!?string[?]
Refer to the most recent command containing string. The trailing ? may be omitted if string is followed immedi-
ately by a newline.
^string1^string2^
Quick substitution. Repeat the last command, replacing string1 with string2. Equivalent to
``!!:s/string1/string2/'' (see Modifiers below).
!# The entire command line typed so far.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
so

awk ' !/ABC/' /path/to/file

should do it from the bash cmd prompt?

HTH,

p5wizard
 
Hi

Depends on which part do you refer : the single quote will help, the leading space not.
Code:
[blue]master #[/blue] echo "This works" > samplefile
[blue]master #[/blue] '!/ABC/' samplefile 
This works
[blue]master #[/blue] awk " !/ABC/" samplefile 
bash: !/ABC/": event not found
[blue]master #[/blue] awk \!"/ABC/" samplefile 
This works

Feherke.
 
Well, you said 'if first unquoted character of a string is an exclamation mark'...

I read the man page:

man bash said:
Several shell options settable with the shopt builtin may be used to tailor the behavior of history expansion. If the histverify shell option is enabled (see the description of the shopt builtin), and readline is being used, history substitutions are not immediately passed to the shell parser. Instead, the expanded line is reloaded into the readline editing buffer for further modification. If readline is being used, and the histreedit shell option is enabled, a failed history substitution will be reloaded into the readline editing buffer for correction. The -p option to the history builtin command may be used to see what a history expansion will do before using it. The -s option to the history builtin may be used to add commands to the end of the history list without actually executing them, so that they are available for subsequent recall.

The shell allows control of the various characters used by the history expansion mechanism (see the description of histchars above under Shell Variables).

Event Designators
An event designator is a reference to a command line entry in the history list.


!
Start a history substitution, except when followed by a blank, newline, = or (.
!n
Refer to command line n.
!-n
Refer to the current command line minus n.
!!
Refer to the previous command. This is a synonym for `!-1'.
!string
Refer to the most recent command starting with string.
!?string[?]
Refer to the most recent command containing string. The trailing ? may be omitted if string is followed immediately by a newline.
^string1^string2^
Quick substitution. Repeat the last command, replacing string1 with string2. Equivalent to ``!!:s/string1/string2/'' (see Modifiers below).
!#
The entire command line typed so far.

so:

awk "! /ABC/" file
awk "\!/ABC/" file
awk '!/ABC/' file
awk \!/ABC/ file

should all be a valid way of specifying this awk program

Another way is to prevent that HISTORY processing wit set +H I believe

set +H
awk "!/ABC/" file
set -H

In any case, it is best to use single quotes around the AWK program string as stated in the awk man page:

aix man page on AIX said:
'Program'
Contains the instructions for the awk command. If the -f flag is
not specified, the Program variable should be the first item on
the command line. It should be bracketed by ' ' (single quotes).


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top