Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3248
  • Last Modified:

Shell Script to loop through a file

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
itsme_asif
Asked:
itsme_asif
1 Solution
 
TintinCommented:
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
 
itsme_asifAuthor Commented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now