Solved

Shell Script to loop through a file

Posted on 2008-10-28
2
3,171 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
ID: 22828199
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
ID: 22828299
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
powershell backup 2 124
shell script or linux command to upload a directory to artifactory? 2 129
Shell Script on AIX 7 96
IE 11 + long running scripts 3 52
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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