1752770 Members
4844 Online
108789 Solutions
New Discussion юеВ

Re: Shell Script

 
SOLVED
Go to solution
ian walker_1
Advisor

Shell Script

Can anybody give me the right syntax for a shell script? I want to open the contents of one file, read through each line and grep for items in another file. Do I use a for or while loop? Can anyone give me the correct syntax to use?

e.g.

cat file1 | while read line
do
???????
???????
done

thanks

9 REPLIES 9
Muthukumar_5
Honored Contributor

Re: Shell Script

You can read file contents from a in serveral ways as,

way 1

touch newfile
cat file1 | while read line;
do
echo $line | grep 'string' >> newfile
done

way 2
(
while read line;
do
echo $line | grep 'string'
done < inputfile
) > newfile

way 3:

grep -E 'string1|string2|stringn' inputfile > newfile

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Shell Script

If you specify the exact requirement then we can give one line solution with perl or sed or awk or grep ;)

hth.
Easy to suggest when don't know about the problem!
ian walker_1
Advisor

Re: Shell Script

thanks for this but its not quite what i want.
Basically I have file1 which contains an output of a sw depot. In file2 I have a list of software that I want to check is in file1... how do I do this?
Leif Halvarsson_2
Honored Contributor

Re: Shell Script

Hi,
I belive you would use the product number in the output from swlist to "grep" for the same product number in the second file.

I suggest using "join" for this task. join matches fields from two files and print out selected fields from both of the files. The files must be sorted , using the matching field as sort key.

For more details, see the man page for join.
ian walker_1
Advisor

Re: Shell Script

ok lets be clear.

file1 contains listing of software depot
file2 contains listing of software product numbers.

I want to, using a loop?, in one go, check the contents of the first file, file1, using file2 as a sort of input or grep. I know you can do this using a for/while loop, but what would be the right syntax???????
Steve Lewis
Honored Contributor
Solution

Re: Shell Script

You don't need a loop. If file2 contains the strings to search file1 for, then its this simple:

grep -f file2 file1

Beware of blank lines in file2.
Neil Wilson_2
Valued Contributor

Re: Shell Script

Hey Ian

try using diff -f > this will eek out wot you need

Neil.
ian walker_1
Advisor

Re: Shell Script

thanks Steve and Neil.
Juan M Leon
Trusted Contributor

Re: Shell Script

Hello Jan, I am under the impression that both files will have product number correct.
by product number use the following

cat file2 | while read line;
do
## line is one field record product number ##
grep -q $line file1
if [ $? -eq 0 ]; then ## if found ##
echo "Product depot $line was fonund in the file1"
else ## if not found ###
tput smso ## reverse video ##
echo "Product $line is not in file1 ..."
tput rmso ## normal video ##
fi
done

I hope this example helps

done