Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 6419  / 3 Years ago, fri, july 2, 2021, 9:07:00

Is there a way to find a specific record among the whole line?



This is my file:



one two three four
two three four five
three four five six
four five six seven
five six seven eight


How do I search for all lines which include two?


More From » awk

 Answers
5
awk '/(^| )two( |$)/' ...


The (..) groups there are trying to ensure we're only matching "two". At the front it either needs to be the beginning of the line or a space and at the end it needs to be a space or the end of the line. In short, we're making sure the field equals two.



Hmm apparently you can also use word-boundary tags (which looks slightly more elegant but isn't as portable):



awk '/<two>' ...


Not sure what your specific use-case is (I assume it's not numbers), you might be just as well off with grep -E '<two>' ... but awk will give you a bit more flexibility if you need to do other stuff.


[#24209] Sunday, July 4, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
uxuriousrnal

Total Points: 279
Total Questions: 106
Total Answers: 96

Location: Fiji
Member since Wed, Mar 29, 2023
1 Year ago
uxuriousrnal questions
Wed, Mar 9, 22, 09:04, 2 Years ago
Mon, Jul 18, 22, 01:48, 2 Years ago
Wed, Apr 13, 22, 01:15, 2 Years ago
Thu, Aug 26, 21, 22:01, 3 Years ago
;