Comments on: How to Use Awk and Regular Expressions to Filter Text or String in Files https://www.tecmint.com/use-linux-awk-command-to-filter-text-string-in-files/ Tecmint - Linux Howtos, Tutorials, Guides, News, Tips and Tricks. Wed, 07 Apr 2021 13:46:50 +0000 hourly 1 By: aparna bl https://www.tecmint.com/use-linux-awk-command-to-filter-text-string-in-files/comment-page-1/#comment-1472126 Wed, 07 Apr 2021 13:46:50 +0000 http://www.tecmint.com/?p=19807#comment-1472126 In reply to Erik Persson.

HI Erik Persson

You are awesome. You taught me awk. No one else explained it like you. Do you teach?

]]>
By: RF Engineer https://www.tecmint.com/use-linux-awk-command-to-filter-text-string-in-files/comment-page-2/#comment-1466138 Wed, 31 Mar 2021 07:05:52 +0000 http://www.tecmint.com/?p=19807#comment-1466138 This is wonderful tutorial and very well illustrated.

There is a minor error in the explanations on what asterisk (*) means in regular expressions — it means ‘match the previous character zero or more times‘. For example 'p*' will match the letter ‘p’ zero or more times, thus this expression will match anything and everything because it will be looking for the letter ‘p’ to be contained zero or more times — and absolutely any text contains the letter ‘p’ either zero or more times.

For this reason asterisk is never used with just a single symbol before it, it must be used in an expression with more symbols inside it, like /A-*B/, which will match “A” followed by zero or more hyphens "-" and then followed by “B”, thus the following strings will produce a match “AB” (this has zero occurrences of ‘-‘), “A-B”, “A–B”, “A—B” and so on.

Note that the text presented in this tutorial erroneously suggests that /A-*B/ will not match “AB” when a simple check in AWK shows that it is matching it (in fact a test in any REGEXP application will show the same result, e.g. egrep).

For this reason this tutorial interprets somewhat erroneously the result of:

# awk '/l*c/{print}' /etc/localhost

The above will match all lines that contain the letter ‘c’ regardless whether they contain the letter ‘l’ or not. This is because /l*/ means ‘match letter l zero or more times‘, so /l*c/ means ‘match letter c preceded by zero or more occurrences of letter l‘, but any line that contains the letter c in it also contain zero or more letters l in front of it — the key to understanding this is “ZERO or more times“.

CONCLUSION: In REGEXP the asterisk symbol (*) does not mean the same thing as in Microsoft Windows and DOS/CMD file-name matching, it does not match any character (as this tutorial erroneously suggests), it matches the preceding character ZERO or more times.

]]>
By: Daniel P Fruzzetti https://www.tecmint.com/use-linux-awk-command-to-filter-text-string-in-files/comment-page-2/#comment-1166290 Tue, 11 Jun 2019 19:42:33 +0000 http://www.tecmint.com/?p=19807#comment-1166290 I’m struggling to make my script work. I have a huge file and each line needs to be searched for the string “WAP” and then, when found, the character appearing two characters BEFORE the string needs to be returned. Can you help me simplify this?

]]>
By: mithran https://www.tecmint.com/use-linux-awk-command-to-filter-text-string-in-files/comment-page-2/#comment-1058324 Mon, 12 Nov 2018 10:04:23 +0000 http://www.tecmint.com/?p=19807#comment-1058324 In reply to Erick Manuel Bazán.

HI,

Can you please provide the script details further, if possible

Thanks & Regards,
Mithran

]]>
By: Aaron Kili https://www.tecmint.com/use-linux-awk-command-to-filter-text-string-in-files/comment-page-2/#comment-1051189 Sun, 21 Oct 2018 07:18:27 +0000 http://www.tecmint.com/?p=19807#comment-1051189 In reply to ROBSON MASSAKI KOBAYASHI.

@ROBSON

Yes, this is true, it matches the whole line.

]]>