awk to remove lines starting with symbol without keyword in them

awk ignore lines starting with
awk exclude character
grep lines starting with
awk not start with
awk start at line
sed
awk exclude multiple pattern
awk start from second line

I am trying to selectively remove lines that start with # but do not contain the keywords Build or Type in them. The lines that do not start with # are unchanged. I can remove all lines that starting with # using the first awk, but not sure how to selectively remove lines that start with # but do not contain a keyword. The second awk does execute but only leaves two lines (#CN Filters: # Flags = 1,2,3). Thank you :).

awk

awk '!/#/' input < out  # will remove all lines with #

awk

awk '/#/ && !/Build|Length/' input < out  # remove lines starting with # but must not have Build or Length in them

input various spacing

#Build = NCBI Build 37
#CN Filters:
#  Flags = 1,2,3
#  Type = Lowess
Length  Event   ID
1   Gain    xxx
10  Loss    yyy

desired output

Build = NCBI Build 37
Type = Lowess
Length  Event   ID
1   Gain    xxx
10  Loss    yyy
$ awk '!/^#/ || /Build|Type/' file
#Build = NCBI Build 37
#  Type = Lowess
Length  Event   ID
1   Gain    xxx
10  Loss    yyy

If you want to remove those initial # characters and the spaces after them:

$ awk '!/^#/ || /Build|Type/ { sub("^#[[:blank:]]*", ""); print }' file
Build = NCBI Build 37
Type = Lowess
Length  Event   ID
1   Gain    xxx
10  Loss    yyy

completely ignore lines that start with a specific pattern, END/ {next} at the top of the script to ignore all lines starting with IRRELEVENT You can't do that with grep; you can do it with sed ( sed '/^IRRELEVENT DATA/,/^​END/' | awk … ) Without using next, using negation instead. The above awk command searches for the lines which starts with ab followed by not of letter(or alphabetic) character one or more again followed by the string cd at the last. If it finds any, then it removes the string ab and cd through awk's built-in gsub function. Finally the result was redirected to standard output.

You want to do something with lines that start with # and do not contain Build or Type, right? I'm sure you could write that condition:

  1. Start with # = /^#/
  2. AND = &&
  3. Do not contains Build or Type = !/Build|Type/

i.e.

/^#/ && !/Build|Type/

Now, what is it you wanted to do when that condition s true? Not print the current line. So you could just write that as simply:

awk '/^#/ && !/Build|Type/{next} 1'

but if you prefer to use awks default print given a true condition then you just need to negate your condition (a{next} 1 = !a):

awk '!(/^#/ && !/Build|Type/)'

which by boolean algebra ( !(a && b) = !a || !b) can be reduced to:

awk '!/^#/ || /Build|Type/'

grep - How to delete lines starting with certain strings, lines not just containing [youtube] , but starting with [youtube] , then add ^ to the pattern, like sed '/^\[youtube\]/d' . But in your case it does not matter. share. 35+ Examples of Regex Patterns Using sed and awk in Linux In this article, we go through a lot of great ways to use Regular Expression, or Regex, Patters, and their applications in sed and awk. by

Following awk may help you on same too.

awk '!(/^#/ && !/Build/ && !/Type/){gsub(/^#|^# +/,"");print}'  Input_file

Explanation:

awk '
!(/^#/ && !/Build/ && !/Type/){  ##Checking condition here if a line starts with # and NOT having string Build and Type in it, Negating this condition to work it as opposite, if this condition is TRUE then do following.
  gsub(/^#|^# +/,"");            ##Using gsub to remove hash in starting of a line OR remove a hash starting fr a line with space with NULL in current line.
  print                          ##Printing the current line here.
}' Input_file                    ##Mentioning the Input_file name here.

How do I delete a line if next line starts with a specific character , So I want to remove those lines (starting with C) which do not have any line below (or add execution permissions to 'chkheadings' and run it without sh). I don't know Perl but, from what I see (and from my awk script), I would guess the  This chapter describes the awk command, a tool with the ability to match lines of text in a file and a set of commands that you can use to manipulate the matched lines. In addition to matching text with the full set of extended regular expressions described in Chapter 1, awk treats each line, or record, as a set of elements, or fields, that can be manipulated individually or in combination.

A sed solution:

$ sed 's/^# *\(.*\(Build\|Type\).*\)/\1/;/^#/d'  file
Build = NCBI Build 37
Type = Lowess
Length  Event   ID
1   Gain    xxx
10  Loss    yyy

Grep only line starting with., Now, I have to grep there informations, like: pre { overflow:scroll; margin:2px; Note that '^1266*' will return lines starting with 1266 followed by any character: Remove new line starting with a numeric value and append it to the previous line How to grep a line not starting with # from a file (there are two lines starting with​  Hi there, I want to trim space between lines in unix. I have a file named abc.txt with 2,00,000 lines.But useful are only a few. Please tell me how to delete the blank lines. | The UNIX and Linux Forums

awk '!/CN|Fl/{sub(/\43/,"")sub(/^\s*/,"");print}' file

Build = NCBI Build 37
Type = Lowess
Length  Event   ID
1   Gain    xxx
10  Loss    yyy

Delete Lines Matching a Specific Pattern in a File using SED , This guide describes how to delete lines matching a specific pattern in a file using SED. To begin with, if you want to delete a line containing the keyword, you If you need to perform a dry run (without actually deleting the line with the keyword) To delete lines starting with a specific character or pattern,  I need to find if any lines in a file begin with **. I cannot figure out how to do it because * is interpreted as a wildcard by the shell. grep -i "^2" test.out works if the line begins with a 2 but . grep -i "^**" test.out obviously doesn't work. (I also need to know if this line ends with a ) but have not attempted that yet).

The GNU Awk User's Guide, It has no effect when FS is a single character, even if that character is a letter. Starting in version 4.2, each field width may optionally be Brian Kernighan's awk and BusyBox awk remove the backslash If the else keyword appears on the same line as then-body and  awk to remove lines that do not start with digit and combine line or lines I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf).

Unix / Linux - Regular Expressions with SED, We will now understand how to delete all lines with sed. Invoke sed When the sed encounters no addresses, it performs its operations on every line in the file. I'm trying to make a program to search for each blank line in a text archive, where blocks are separated by this blank lines. I want to write the number of the line in a new file "numblank.txt" I have this, but I think I have a problem with the "blank line" simbol.

20 awk examples – Linux Hint, It can read any file content line by line and separate the fields or columns If the value of the output is less than 10 characters then the spaces will be BEGIN keyword is used in the script for informing awk command to execute The following command will search any word that is not starting with 'F' but ending with 'ool'.

Comments
  • Thank you both very much :).
  • @Chris, glad that it helped you, added explanation too here, cheers.