Awk regular expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk regular expressions
# 1  
Old 03-30-2009
Awk regular expressions

Hi Experts,

Can you please help me out for the below scenario,

I have a variable length file with the fixed number of columns, in which the fields are delimited by pipe symbol (|). From that file I have to extract the lines which has the following scenario,
The field1 in a line should have the field2 as its part.
For example,
sample.out
A123(BCD)|BCD|DEFGH||WE|
C234|54CD|DFVCSE|W|TY|

From the above sample file, I have to extract the line A123(BCD)|BCD|DEFGH||WE| , in which the field1 has the value of field2 enclosed in brackets.

Hope I am clear.

I have tried using awk -F'|' '{if( $1 ~ /$2/ ) {print $0;}}' sample.out
This is not working for me.

Kindly help me out for this.

Thanks in advance,

Chella.
# 2  
Old 03-30-2009
Try this:

Code:
awk -F"|" '$1 ~ $2' file

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expressions

I need to pick a part of string lets stay started with specific character and end with specific character to replace using sed command the line is like this:my audio book 71-skhdfon1dufgjhgf8.wav' I want to move the characters beginning with - end before. I have different files with random... (2 Replies)
Discussion started by: XP_2600
2 Replies

2. Shell Programming and Scripting

awk or sed or python for regular expressions ?

Linux 6.X environments (RHEL, Oracle Linux ) I could write basic shell scripts in bash. In my spare time, I was planning to learn awk or sed to deal with regular expression tasks I have to deal with. But, I gather that python is gaining popularity these days and I came to know that python has a... (5 Replies)
Discussion started by: John K
5 Replies

3. Shell Programming and Scripting

Assistance required with awk and regular expressions

Hello there, I am trying to get my head around the section below of a script we use that incorporates AWK and Regular Expressions. { match($0,"The broker*");print $1,$2,$3 ":", substr($0, RSTART,RLENGTH)} I have a basic understanding of how match works, what I am struggling with is the... (2 Replies)
Discussion started by: jimbojames
2 Replies

4. Shell Programming and Scripting

Regular Expressions

Hi Ilove unix and alwyas trying to to learn unix,but i am weak in using regular expressions.can you please give me a littel brief discription that how can i understand them and how to use .your response could lead a great hand in my unix love. (1 Reply)
Discussion started by: manoj attri
1 Replies

5. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

6. Shell Programming and Scripting

Test Regular Expressions on Arrays in Awk

How would I test for a suffix on an element in an array? e.g. testing for /$html/ of an element array (4 Replies)
Discussion started by: ROFL
4 Replies

7. Shell Programming and Scripting

Handling regular expressions in awk

Script is: accept filename as argument(also handle CTRL+C).to check whether th file exist in the current directory,it it then using awk find the employees who are either born in 1990 or drawing a salary greater than 25000. In my database file 1st field is of id ,2nd field is name,5th field is of... (5 Replies)
Discussion started by: Priyanka Bhati
5 Replies

8. Shell Programming and Scripting

Regular Expressions

#!/usr/bin/perl $word = "one last challenge"; if ( $word =~ /^(\w+).*\s(\w+)$/ ) { print "$1"; print "\n"; print "$2"; } The output shows that "$1" is with result one and "$2" is with result challenge. I am confused about how this pattern match expression works step by step. I... (8 Replies)
Discussion started by: DavidHe
8 Replies

9. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

10. Shell Programming and Scripting

regular expressions

Hello, Let say I have a string with content "Free 100%". How can extract only "100" using ksh? I would this machanism to work if instead of "100" there is any kind of combination of numbers(ex. "32", "1238", "1"). I want to get only the digits. I have written something like this: ... (4 Replies)
Discussion started by: whatever
4 Replies
Login or Register to Ask a Question