I need to check if 2 values exists in the file and if they are equal print 0.
output.txt:
------------
Inputs:
My pattern matching code works but I am trying to set a counter if both the pattern matches which does not work.If the count > 0,then I want to check if $a==$b
Please point out my mistake
Last edited by Don Cragun; 10-14-2015 at 02:23 PM..
Reason: Add CODE and ICODE tags.
Welcome to forums hope you will enjoy learning here. Please use code tags as per forum rules for Inputs/codes/commands which you are using into your post as per forum rules. Now coming to your post, $a will denote value of a variable but in awk it wouldn't work like that way, there is NO need of $ sign to get the value of variables here. An example for this is as follows.
Output will be as follows.
Now for your requirement as far as I understood your requirement, you have 2 variables named a and b and if there value is present in file(Taking your sample file only, because if file has multiple lines then we need to be more specific about your requirement as you need to tell us either you need to check variable in whole file or line by line is fine.) and their own values are equal then print value 0 in output.
Because I have taken an example where values or variable a and b are same it will show output as follows.
If you have any doubts or your requirement is not meeting this solution, I would suggest you to show your sample input with all conditions with their expected output and your O.S details too, hope this helps you.
shell variables (your a and b ) are not available in awk per se. On top, single quoting prevents shell expansion. So it's difficult to believe that your first sample code worked.
In awk, /.../ is a regex constant; you can't have variables in there $0~/$a/ and /$b/ are equivalent (but would not work as expected!) {...} is an action, not a pattern (so {count++} will always be executed)
Try
You'll still need to make sure there are no false positives (as "100" would match "1")
I'm facing a problem
1) I got a list_file intended to be used for inlace replacement like this
Replacement pattern ; Matching patterns
EXTRACT ___________________
toto ; tutu | tata | tonton | titi
bobo ; bibi | baba | bubu | bebe
etc. 14000 lines !!!
... (5 Replies)
Hello all,
I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk.
I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field... (2 Replies)
Hello.
grep v2.21
Debian 8
I wish to search for and output these patterns in order;
"From " "To: " "Subject: " "Message-Id: " "Date: " "To: "
grep works, but not in strict order...
$ grep -a -E "^From |^Subject:|^From: |^Message-Id: |^Date: |^To: " InboxResult;
From - Wed Feb 18... (10 Replies)
Hi forum,
Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result?
Rules: Must match the 3 line pattern and return a 1 line result.
I have found solutions with sed, but it seems that sed installed in my system is... (5 Replies)
Hi,
I wasn't quite sure how to title this one! Here goes:
I have some already partially parsed log files, which I now need to extract info from. Because of the way they are originally and the fact they have been partially processed already, I can't make any assumptions on the number of... (8 Replies)
Hi,
I am using sub to remove blank spaces and one pattern(=>) from the input string. It works fine when I am using two sub functions for the same. However it is giving error while I am trying to remove both spaces and pattern using one single sub function.
Working:
$ echo " OK => " |awk... (2 Replies)
Hi,
I have a file that is 430K lines long. It has records like below
|site1|MAP
|site2|MAP
|site1|MODAL
|site2|MAP
|site2|MODAL
|site2|LINK
|site1|LINK
My task is to count the number of time MAP, MODAL, LINK occurs for a single site and write new records like below to a new file
... (5 Replies)
Hi there,
We have been given a bit of coursework using awk on html pages. Without giving too much away and risking the wrath of the plagerism checks, I can say we need to deal with certain html elements.
There may be several of these elements on one line. My question is, if there are more... (1 Reply)