Multiple patterns for awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple patterns for awk script
# 1  
Old 11-18-2014
Linux Multiple patterns for awk script

Hi,

I'm getting stuck when supplying multiple patterns for the below code:

Code:
awk -F, '
..
..
if ($0 ~ pattern)
{ .. .. }
..
..
' pattern='$ROW' input_file

for the same code I'm trying to supply multiple patterns as given below:
Code:
awk -F, '
..
..
if( ($0 ~ pattern) && ($0 ~ pattern1))
{ .. .. }
..
..
' pattern='$ROW' pattern1='$COLUMN' input_file

The script is running fine with no errors but its not giving expected result.

Kindly help me in solving this.
# 2  
Old 11-18-2014
pattern='$ROW' pattern1='$COLUMN'
I'm guessing the single quotes are a copy/paste artifact?
# 3  
Old 11-18-2014
In an RE a $ is the line end - no more characters may follow.
But you certainly want the shell to substitute shell variables; then put them in double-quotes:
pattern="$ROW" pattern1="$COLUMN"
# 4  
Old 11-18-2014
$ROW AND $COLUMN are not variables.

They are just the data present inside their input_file.

I need to add few more patterns which are just numbers and alphabets only like '1222001' ,'negative'. So giving it inside double quotes will solve this issue ?

Just also wanted to confirm whether the way I'm doing is right ?
# 5  
Old 11-18-2014
Quote:
Originally Posted by penqueen
... So giving it inside double quotes will solve this issue ?
Just also wanted to confirm whether the way I'm doing is right ?
Why not just try it with a short example and see for yourself?

Code:
$
$
$ # The data file
$ cat f2
ROW=23,COL=56,DATA=The quick brown fox
ROW=10,COL=29,DATA=jumps over
ROW=9,COL=78,DATA=the lazy dog
$
$ # The shell variables
$ ROW=10
$ COLUMN=29
$
$ # Using single quotes
$ awk -F, '{if ($0 ~ pattern && $0 ~ pattern1) {print}}' pattern='$ROW' pattern1='$COLUMN' f2
$
$ # Using double quotes
$ awk -F, '{if ($0 ~ pattern && $0 ~ pattern1) {print}}' pattern="$ROW" pattern1="$COLUMN" f2
ROW=10,COL=29,DATA=jumps over
$
$

# 6  
Old 11-18-2014
Quote:
Originally Posted by penqueen
$ROW AND $COLUMN are not variables.

They are just the data present inside their input_file.

I need to add few more patterns which are just numbers and alphabets only like '1222001' ,'negative'. So giving it inside double quotes will solve this issue ?

Just also wanted to confirm whether the way I'm doing is right ?
If you want to search for the literal strings with $ then you need to escape the $,
pattern='\$ROW' pattern1='\$COLUMN' or pattern='[$]ROW' pattern1='[$]COLUMN'

---------- Post updated at 01:39 PM ---------- Previous update was at 01:33 PM ----------

Or search for literal strings (no RE) with
Code:
awk -F, '
..
..
if (index($0,pattern) > 0 && index($0,pattern1) > 0)
{ .. .. }
..
..
' pattern='$ROW' pattern1='$COLUMN' input_file

# 7  
Old 11-18-2014
How about providing a sample of the input and desired output...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Replace Multiple patterns within a list_file with One in target_file

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)
Discussion started by: mpvphd
5 Replies

2. Shell Programming and Scripting

Replacing matched patterns in multiple files with awk

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)
Discussion started by: karlmalowned
2 Replies

3. Shell Programming and Scripting

Check multiple patterns in awk

I need to check if 2 values exists in the file and if they are equal print 0. output.txt: ------------ 1 2 3 4 5 6 Inputs: a=1 b=2 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... (3 Replies)
Discussion started by: kannan13
3 Replies

4. Shell Programming and Scripting

Replacing multiple line patterns with awk

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)
Discussion started by: demmel
5 Replies

5. Shell Programming and Scripting

awk extract strings matching multiple patterns

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)
Discussion started by: chrissycc
8 Replies

6. Shell Programming and Scripting

Searching multiple patterns using awk

Hello, I have the following input file: qh1adm 20130710111201 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qx1adm 20130711151154 : tp count QX1 u6 -Dsourcesystems=B17,E17,EE7 qh1adm 20130711151155 : tp import all... (7 Replies)
Discussion started by: kcboy
7 Replies

7. Shell Programming and Scripting

[Solved] HP-UX awk sub multiple patterns

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)
Discussion started by: sai_2507
2 Replies

8. Shell Programming and Scripting

Awk to Count Multiple patterns in a huge file

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)
Discussion started by: reach.sree@gmai
5 Replies

9. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

10. UNIX for Dummies Questions & Answers

AWK: Multiple patterns per line

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)
Discussion started by: Plavixo
1 Replies
Login or Register to Ask a Question