shell & AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell & AWK
# 1  
Old 09-24-2008
shell & AWK

Hi,

I want to make a script using shell and awk together, the objective is----
Using for loop I am reading data from a file and then want to print (using awk) the first and fifth field from a directory.
This script is not running as it should........
Please helpSmilie
Thanks in advance.........

for i in `cat file1` ;
do
grep $i /user2/INAUTO/LOG/OCS*24092008* | awk '/Success/ { print $1,$5}' *24092008*;
done
# 2  
Old 09-24-2008
Is the grep supposed to tell you which files contain a pattern from file1, and run awk on those which do? You need grep -l to list file names, and awk should run with xargs or something to get only the files returned by grep.

Code:
awk '/Success/ { print $1, $5 }' $(grep -lf file1 /usr2/INAUTO/LOG/OCS*24092008*)

If your shell is old, you might need to use `grep ...` with grave accents (aka backticks, ASCII 96 -- not straight quotes!) instead of $(grep ...)

Another change is to use grep -f file1 to read all patterns from file1 in one go. If your grep doesn't support the -f option, or if file1 contains too many patterns for your grep, you will obviously have to undo that part.

You could probably refactor this further to a single awk script which collects output from one file at a time, and only prints the Success lines once it has determined a match on one of the patterns in file1.
# 3  
Old 09-24-2008
Quote:
Originally Posted by tushar_tus
Hi,

I want to make a script using shell and awk together, the objective is----
Using for loop I am reading data from a file and then want to print (using awk) the first and fifth field from a directory.
This script is not running as it should........

What does "not running as it should" mean? Describe what you want to happen and what actually happens. Post any error messages you get (copy and paste, do not retype them).
Quote:
for i in `cat file1` ;
DON'T DO THAT!

That may or may not read the file line by line.

If you want to read a file line by line, use a while loop:

Code:
while IFS= read -r line
do
 : do whatever with "$line"
done < FILE

Quote:
do
grep $i /user2/INAUTO/LOG/OCS*24092008* | awk '/Success/ { print $1,$5}' *24092008*;
done
# 4  
Old 09-24-2008
Re:

grep -f is not working..
Is there any alternate for this or a total different awk script to achieve the objective.......Smilie
# 5  
Old 09-25-2008
Quote:
Originally Posted by tushar_tus
grep -f is not working..
Is there any alternate for this or a total different awk script to achieve the objective.......Smilie

What does "not working" mean? Please explain what happens and why that is not what you want.

If you get error messages, post them exactly as they appear; do not retype them.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

awk and &&

Hello Everybody. I am having trouble matching multiple strings within an instanza. I ONLY want it to print if it finds both strings within the range. Example input (newlines between groups inserted only for readibility): Keywords to match are "artist" and "play count" startgroup1 artist:... (5 Replies)
Discussion started by: sudo
5 Replies

3. UNIX for Dummies Questions & Answers

Implement the '&&' function in a shell

Hello, I'm currently implementing the && function in a shell using C. For example, if we input cmd1 && cmd2, then cmd2 executes only when cmd1 exits successfully. I'm thinking about: int main() { int i; char **args; while(1) { printf("yongfeng's shell:~$ "); args =... (5 Replies)
Discussion started by: Yongfeng
5 Replies

4. Shell Programming and Scripting

awk Help: quick and easy question may be: How to use &&

Hi Guru's. I am trying to use to check if $5 is greater than 80 & if not 100, then to print $0 : awk '{ if ($5>80) && if ($5 != 100) print $0} But getting error: >bdf1|sed 's/%//g'|awk '{ if ($5>80) && if ($5 != 100) print $0}' syntax error The source line is 1. The error... (6 Replies)
Discussion started by: rveri
6 Replies

5. Shell Programming and Scripting

Bourne shell & Korn shell

Could some one tell me the difference btw Bourne shell and the Kshell? Which is more flexible and reliable in terms of portability and efficiency. When i type the following command .. $ echo $SHELL yields me /bin/sh Does this tells me that I am in Bourne shell. If yes, how can i get... (6 Replies)
Discussion started by: bobby1015
6 Replies

6. Shell Programming and Scripting

Shell Variables & awk...Help Please

I apologize if this topic has been beaten to death here, but my limited searching skills did not throw up any results. Here's what I am trying to accomplish List all the files in a certain directory; assign the file names to an array which will be used later in the script. My script looks like... (2 Replies)
Discussion started by: kash80
2 Replies

7. Shell Programming and Scripting

awk and &&

Hi I need to make some checks inside an awk statement, but cannot come up with the the syntax for several checks under one awk statement, for example: grep -w ${bits} mydata | awk '{if ($3==1)&&($4==3)&&($5==1) print }' awk: syntax error near line 1 awk: illegal statement near line 1 In... (3 Replies)
Discussion started by: aoussenko
3 Replies

8. UNIX for Advanced & Expert Users

Network Shell Script & Blade Logic & Network Security

I am going to take up a position in Data & Network Security. I would need to write network shell scripts doing the following task: Going to around 2000 servers and findout which groups has access to each servers and which ids are there in each group that has access. I need to implement... (1 Reply)
Discussion started by: pinnacle
1 Replies

9. Shell Programming and Scripting

C shell & Bourne Shell

Hi Guys, My first post and simple one at that .. Really rusty with this shell scripting..\ I have a script called .. j.sh I am calling > j.sh LOG_PATH $BLMBRGDATA/blmbrg.properties where j.sh is grep $1 $2 | cut -d',' -f2 . $BLMBRGDATA is set to a directory path. why do i get :- $... (3 Replies)
Discussion started by: jsm66
3 Replies

10. Shell Programming and Scripting

Awk: Version && nextfile

How can I find which version of Awk is installed? OpSystem is HPUX 11.x I am getting an error when trying to use the keyword nextfile and I dont know why! (Well, I can only assume that I have am using a version of Awk that does not support nextfile. However, according to O'Reilly, nextfile is... (3 Replies)
Discussion started by: google
3 Replies
Login or Register to Ask a Question