regex - Awk only match first match of line in multiline text -


I have a specific number ( 06:00 ) and ( 9:00 ) If this is the first match on a line in a multi-line file, the problem is that the awk about my limited knowledge is that I either get only the first match, or even the second number also matches I would also like to match the counting and ending of the resulting matches, but since it does not match correctly, I have not yet met.

  in the schedule & lt; 06: 00>: 12 lieutenant; Written in 0: 06: 00; 08: 00 & gt; 10 out of & lt; 06: 00 & gt ;: 0; 06: 00 & gt ;: 09 Out & lt; 05: 00 & gt ;: 0 in the schedule & lt; 07: 00 & gt ;: 13 out & lt; Schedule: 08: 00 & gt;: 0; 06: 00>: 12 out of & lt; 09: 00 & gt ;: 0; 09: 00 & gt ;: 12 Out & lt; Written in 0: 06: 00; 07: 00>: 11 out of & lt; 06: 00 & gt ;:   

I tried:

  awk '/ 06 / || / 09 / 'schedule.txt awk' $ 1 ~ / \ & lt; 06 / || / \ & Lt; 09 / {print $ 1} 'schedule.txt   

Correct output: 06: 00> 12 out & lt; 06: 00 & gt ;: Schedule in 0; 06: 00 & gt ;: 9 outside & lt; 05: 00 & gt ;: 0 schedule lieutenant; 06: 00 & gt; 12 out & lt; 09: 00 & gt;: 0: 09: 00 & gt ;: 12 Out & lt; 06: 00 & gt ;: 4 4 matches

About:

  awk '$ 3 ~ / & lt; 06 / || / & Lt; 09 / {print $ 0} 'schedule.txt # ^ ^   

The main point by default awk to string each string Is in the $ 1 in the first word "word", $ 2 in words separated from spaces and so on. The complete line is $ 0 .

Looking at one of your input lines:

  in the schedule & lt; 06: 00 & gt ;: 12 Out & lt; 06: 00 & gt;: 0 & lt; ------ & gt; & Lt; & Gt; & Lt; -------- & gt; & Lt; - & gt; & Lt; ------- & gt; $ 1 $ 2 $ 3 $ 4 $ 5 & lt; ------------------------------ & gt; $ 0   

I have changed your awk program to test $ 3 which appears to be the correct field and matches In the case of, I print the entire line ( $ 0 ).

As a note, print $ 0 can be minimized print . Many orders / tasks that accept the string with some more "frills" in awk $ 0 sh $ awk '$ 3 ~ / & lt; 06 / || / & Lt; 09 / {count ++; Print $ 0} END {printf ("% d total match \ n", counting)} 'in schedule.txt & lt; 06: 00 & gt ;: 12 Out & lt; 06: 00 & gt ;: 0; 06: 00 & gt; : 9 out of & lt; Schedule: 0: 00: 0; 06: 00>: 12 out of & lt; 09: 00 & gt ;: 0; 09: 00 & gt; 12 & amp; 0 06: 00 & gt; Altogether,


Finally, @jedar said as follows:

"All this can be reduced:"

  awk 'END {print X, "total match"} $ 3 ~ / 0 [6 9]: / & amp; ++ x '    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -