Handling regular expressions in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling regular expressions in awk
# 1  
Old 12-02-2009
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 salary and 4th field is of date of birth in the format dd/mm/yyyy.

Error is: Unexpected end of file and unable to match pattern "'.

Please check my code and also suggest me how to handle CTRL+C



code :
Code:
if test -e $1
then
        echo "File Exist" in the current directory"
fi
echo "Employee details born in 1990 or drawing a salary greater than 25000"
awk -F"|" '$5 >25000 || $4 ~/90$/
{
        printf "%d %s %s %d\n", $1,$2,$4,$5
}' emp.lst


Thanks in advance.

Last edited by pludi; 12-02-2009 at 04:35 AM.. Reason: code tags, please...
# 2  
Old 12-02-2009
You do have an extra quote in this line:

Code:

echo "File Exist" in the current directory"

And you should probably quote $1 here:
Code:

if test -e "$1"

What is the purpose of handling Control-C?

Code:
trap "echo do something here...." 2


Last edited by Scott; 12-02-2009 at 05:12 AM..
# 3  
Old 12-02-2009
problem in awk scripts

Thanks..... that quote problem is solved
but the script written in awk is not printing the output.....only printing the correct value for 1st field rest 0 is printing.My databas file is emp.lst and i have put a space between > and 25000 then also not working.....please help me out.

i run this script as: sh empawk.awk emp.lst

empawk.awk is my code file.
# 4  
Old 12-02-2009
The only problem with your awk that I can see is that the { should not be on a newline:

Code:
awk -F"|" '$5 > 25000 || $4 ~/90$/ {
        printf "%d %s %s %s\n", $1,$2,$4,$5
}

Can you show a sample of your input? Is it really pipe-separated?

(also, if you have nawk, try that too, instead of awk!)
# 5  
Old 12-02-2009
Priyanka,

I tried this and it is working fine,

Code:
if test -e $1
then
        echo "File Exist in the current directory"
fi
echo "Employee details born in 1990 or drawing a salary greater than 25000"
awk -F"|" '$3 > 25000 || $4 ~/90$/ \
{
        printf "%d %s %s %d\n", $1,$2,$4,$5
}' emp.lst


Last edited by Neo; 12-02-2009 at 08:09 AM.. Reason: please use code tags!
# 6  
Old 12-02-2009
handling regular expression in awk

Thanks a lot scottn
u were right..... my input is not pipe separated.i made the fields pipe separated..then the same code is working fine........
thanks a lot again....
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

Regular expressions help

need a regex that matches when a number has a zero (0) at the end of it so like 10 20 120 30 330 1000 and so on (6 Replies)
Discussion started by: linuxkid
6 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

Regular Expressions

what elements does " /^/ " match? I did the test which indicates that it matches single lowercase character like 'a','b' etc. and '1','2' etc. But I really confused with that. Because, "/^abc/" matches strings like "abcedf" or "abcddddee". So, what does caret ^ really mean? Any response... (2 Replies)
Discussion started by: DavidHe
2 Replies

8. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: chella
1 Replies

9. UNIX for Advanced & Expert Users

Regular Expressions

Hi, below is a piece of code written by my predecessor at work. I'm kind of a newbie and am trying to figure out all the regular expressions in this piece of code. It is really a tough time for me to figure out all the regular expressions. Please shed some light on the regular expressions... (3 Replies)
Discussion started by: ramky79
3 Replies

10. Shell Programming and Scripting

Regular Expressions

I'm trying to parse RichText to XML. I want to be able to capture everything between the '/par' tag in the RTF but not include the tag itself. So far all I have is this, '.*?\\par' but it leaves '\par' at the end of it. Any suggestions? (1 Reply)
Discussion started by: AresMedia
1 Replies
Login or Register to Ask a Question