Solved

Shell Script to loop through a file

Posted on 2008-10-28
2
3,152 Views
Last Modified: 2013-12-26
Hi Experts,
 Have a dumb question, i have the following script which loops through a file and takes the word in each line of that file and does a grep on a different file, however instead of grep'ing through every single line it only gives the grep output of the last line.
#!/usr/bin/sh
 
 

# Loop through the file

for i in `cat inp.txt`

do
 
 

echo $i

grep $i it_privilege-export.csv|awk -F "," '{print $1}'

done 
 
 
 

exit 0;

Open in new window

0
Comment
Question by:itsme_asif
2 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
Comment Utility
I can't see any logical reason why you'd be getting just a single line from grep if you have multiple matches.

BTW, you can replace your script with




grep -f inp.txt it_privilege-export.csv | awk -F, '{print $1}'

Open in new window

0
 

Author Comment

by:itsme_asif
Comment Utility
Hi , I figured its due to the presence of trailing space in my inp file, i removed using a sed command and things are fine now, thanks for the one liner, thats pretty usefull.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now