shell script
how do i open a file in a shell script and then read file per line.. and check if that read line has a specific string..
thanks in advance
Linux
how do i open a file in a shell script and then read file per line.. and check if that read line has a specific string..
thanks in advance
Something like:
for i in `cat filename`
do
grep $i
done
Hope this helps.
by jerome
cat input_file | while read line
do
x=`echo $line | grep "specific string"`
if [ "$x" -ne "" ]
then
do what you want to do here....
fi
done
by jerome
by d. brock
Using korn shell:
#!/bin/ksh
FILE="filename"
CONTENT=""
set -A LINE
COUNT=0
COUNT2=0
WORD=""
getContent()
{
# loop through config file
while read CONTENT
do
# read and count all lines, place them into an array
let "COUNT=COUNT + 1"
LINE[$COUNT]=$CONTENT
done < $FILE
}
compare()
{
COUNT=0
while [ $COUNT -le $COUNT1 ]
do
let "COUNT1=COUNT1 + 1"
if [ "$LINE[COUNT]" = "whatever" ] ; then
stuff here ;
fi
done
}
getContent
compare
# End
This isn't as compact as it could be but should be easy to read and maintain, and should be useful in other scripts. I use something similar as part of a scaner/parser script.
by d. brock
opps, should be
while[$COUNT1 -le COUNT]
Logic bugs bite.
by d. brock
And remove the COUNT=0 in compare()
Also, if["$LINE[COUNT]" = "whatever" ] ;then
souldbe:
if ["$LINE[COUNT1]" = "whatever" ] ; then
I appologize for the logic bugs, they are of the cut-and-paste-without-a-brain type.
by jerome
by jerome
This discussion is closed to new replies.
Sort by Trending