swam
swam

Reputation: 303

Bash Scripting- Grep/Awk output as input to another script

I have a script that runs against a file and that takes arguments from a user and is sent through grep and awk to get the desired column. It will return multiple lines. What I want to do is read (or something of the like) through the output from grep|awk and run that through another script (./lookup). My question is how can I loop through each line and run what is on that line through my script inside the script. Hope this makes sense, I'm new to scripting and linux.

#!/bin/sh
x=$(grep "$*" "$c"|awk '{print $6}')
while read LINE   
do
./lookup $x
done

This seems to work but only for the first line of the output. It also requires me to hit enter to get the output from the ./lookup script. Any ideas or help? Thanks!

Upvotes: 1

Views: 3527

Answers (2)

user3442743
user3442743

Reputation:

You could also use xargs instead of a loop

Also as barmar stated there is not need for grep.

awk -va="$*" '$0~a{print $6}' "$C" | xargs ./lookup

Upvotes: 1

Barmar
Barmar

Reputation: 781814

Pipe the output of grep and awk to the loop:

grep "$*" "$c" | awk '{print $6}' | while read LINE
do
    ./lookup "$LINE"
done

BTW, it's not usually necessary to use both grep and awk, since awk can do regexp matching. So you can write:

awk -v re="$*" '$0 ~ re {print $6}' "$c" | while ...

The -v option sets an awk variable, and the ~ operator performs regular expression matching. $0 refers to the whole input line.

Upvotes: 1

Related Questions