Discussion Locked

shell script

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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

9 replies

by ghislain.levesque

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 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

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.

This discussion is closed to new replies.