exact match in awk
Dear Experts,
I have a file including number "78" and "178" at different unknown positions in the text. I want to match the line including "78" and do something to strings in that line. When I use Code:
awk 'match($0, "78"){action}'FILENAME I should not use pattern Code:
$Filed == "78" So, how can I match exactly "78" but not "178" with "match"? Any help would be greatly appreciated. Thanks a lot for your time! |
You can try word boundaries. In awk they are specified by the \y operator and you need to use a regular expression (enclosed in slashes) instead of a string constant:
Code:
awk 'match($0, /\y78\y/)' file |
Quote:
|
OK, I find a solution myself like:
Code:
awk 'match($0, /^78/) {action}' FILENAME Code:
awk '/^78/ {action}' FILENAME But now if I want to incorporate this code in a bash script. The 78 will be replaced by a variable, say x, according to my goal. Then I tried Code:
x=78 Code:
x=78 Code:
awk -v y=$x '/y/ {action}' FILE Sorry for the further more questions. I really hope to solve it completely and makes things clear. Any help would be appreciated! |
Try to keep $x outside single quotes:
Code:
awk 'match($0, /^'$x'/)' FILE |
So I find it interesting in your first post you say the position of 78 is unknown and yet the solution is to say it is at the start of the line??? I would consider this
knowing the position. Also have you considered (or maybe you do not need to) what happens if you have 785 at the start of a line? Lastly, match is not required as you save no data of the match so a computed regex will do fine: Code:
awk -v y=$x '$0 ~ "^"y{action}' FILE Code:
awk -v y=$x '$1 == y{action}' FILE |
perfect answer
Quote:
785 might also possibly appeared, so I also need to avoid it. I just only need the line have "78", it can have any other characters which I do not care as long as "78" is there. That's why I need EXACT match. Thanks a lot for your code. The first one is exactly what I am looking for! Works like a champ and perfectly fit the whole script so that I do not have to change anything else. Thanks a lot! |
I am happy you have a solution but you have contradicted yourself in the answer. If my solutions work then 78 must be at the start of a line. It will not find 78
anywhere else such as: Quote:
Good luck. |
Quote:
But, thanks anyway, I learnt lot from you. |
I wonder why
Code:
awk '/\y78\y/' FILE Code:
x=78 |
So, still unsolved.
If 78 is not at the beginning of the line. Nothing would work. Code:
So, still I need an answer for the EXACT match. Sorry for those useless and misleading replies I have posted. My faults. If anyone has still some idea on this topic, please post your comment. Any answer would be appreciated. Sorry again for your time and thanks all the same. |
I would stick with the regexp with word boundary solution, trying to understand why they didn't work for you. Which version of awk are you running and on which *nix OS?
|
The real file looks like:
Code:
|
Quote:
I use ubuntu 10.04. The awk I believe is Mawk. Would you please have some further comments? Thanks again! |
Quote:
|
All times are GMT -5. The time now is 12:07 PM. |