Print lines from match until end of file with awk

fedorqui 'SO stop harming'

In Print lines in file from the match line until end of file I learnt that you can use 0 in range expressions in awk to match until the end of the file:

awk '/matched/,0' file

However, today I started playing around this 0 and I found that this also works:

awk '/matched/,/!./' file

I guess 0 is working because in the range expression /regex1/,/regex2/ the lines are printed until the regex2 evaluates as True. And since 0 evaluates as False, this never happens and it ends up printing all the lines. Is this right?

However, with /!./ I would imagine this to work until a line without any character is found, like if we said awk '/matched/,!NF'. But it does not: instead, it always evaluates to False. What is the reasons for this?

Examples

$ cat a
1
matched
2

3
4
end

Both awk '/matched/,/!./' a and awk '/matched/,0' a return:

matched
2

3
4
end

Whereas awk '/matched/,!NF' a returns:

matched
2

anubhava

You have exclamation at wrong place. Keep it outside the regex like this to negate the match:

awk '/matched/,!/./' a
matched
2


Your regex /!./ is matching a literal ! followed by any character.

If your file is this:

cat a
1
matched
2

3
!4
end

Then

awk '/matched/,/!./' a
matched
2

3
!4

And:

awk '/matched/,!/./' a
matched
2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

awk: print lines after match to end of file

From Dev

awk + print lines from the first line until match word

From Dev

print specified amount of lines in file until hitting the end

From Dev

awk: print lines that DO NOT match patterns in a file, looking at a specific column

From Dev

Get lines from file that match strings in another file using AWK

From Dev

delete all lines from match line until end include the match line

From Dev

awk exact variable match, print remaining lines

From Dev

awk print 2 lines back if match

From Dev

match pattern and print corresponding columns from a file using awk or grep

From Dev

awk to print from last match of start match pattern till end pattern

From Dev

awk to print from last match of start match pattern till end pattern

From Dev

extract lines from bottom until regex match

From Dev

Print the duplicate lines in a file using awk

From Dev

print lines with blank in file using awk

From Dev

Awk to print lines in file with multiple delimiters

From Dev

Removing from the end of the file until you see a number or letter with sed/awk

From Dev

Grep a pattern in a line from log file and print next n lines until next patter

From Dev

using awk to match and sum a file of multiple lines

From Dev

awk: how to append lines in a file until finding a file separator symbol?

From Dev

awk command to delete duplicate lines from tcl file and print into another tcl file

From Dev

How to use grep/awk/unix to match all lines from one file in another file, even if they are duplicates

From Dev

awk compare 2 files, print match and nonmatch lines;3rd column of first file and 2nd column of second file

From Dev

Print range - print from page x until end of document

From Dev

awk compare 2 files, print match and nonmatch lines

From Dev

awk find the last match and print the next N lines

From Dev

awk compare 2 files, print match and nonmatch lines

From Dev

awk find the last match and print the next N lines

From Dev

awk print only lines between two patterns removing first match

From Dev

Remove Lines that Match from File

Related Related

  1. 1

    awk: print lines after match to end of file

  2. 2

    awk + print lines from the first line until match word

  3. 3

    print specified amount of lines in file until hitting the end

  4. 4

    awk: print lines that DO NOT match patterns in a file, looking at a specific column

  5. 5

    Get lines from file that match strings in another file using AWK

  6. 6

    delete all lines from match line until end include the match line

  7. 7

    awk exact variable match, print remaining lines

  8. 8

    awk print 2 lines back if match

  9. 9

    match pattern and print corresponding columns from a file using awk or grep

  10. 10

    awk to print from last match of start match pattern till end pattern

  11. 11

    awk to print from last match of start match pattern till end pattern

  12. 12

    extract lines from bottom until regex match

  13. 13

    Print the duplicate lines in a file using awk

  14. 14

    print lines with blank in file using awk

  15. 15

    Awk to print lines in file with multiple delimiters

  16. 16

    Removing from the end of the file until you see a number or letter with sed/awk

  17. 17

    Grep a pattern in a line from log file and print next n lines until next patter

  18. 18

    using awk to match and sum a file of multiple lines

  19. 19

    awk: how to append lines in a file until finding a file separator symbol?

  20. 20

    awk command to delete duplicate lines from tcl file and print into another tcl file

  21. 21

    How to use grep/awk/unix to match all lines from one file in another file, even if they are duplicates

  22. 22

    awk compare 2 files, print match and nonmatch lines;3rd column of first file and 2nd column of second file

  23. 23

    Print range - print from page x until end of document

  24. 24

    awk compare 2 files, print match and nonmatch lines

  25. 25

    awk find the last match and print the next N lines

  26. 26

    awk compare 2 files, print match and nonmatch lines

  27. 27

    awk find the last match and print the next N lines

  28. 28

    awk print only lines between two patterns removing first match

  29. 29

    Remove Lines that Match from File

HotTag

Archive