rodrick
rodrick

Reputation: 1

Linux script to search for string in a file

I am newbie to shell scripting. I have a requirement to read a file by line and match for specific string. If it matches, print x and if it doesn't match, print y.

Here is what I am trying. But,I am getting unexpected results. I am getting 700 lines of result where my /tmp/l1.txt has 10 lines only. Somewhere, I am going through the loop. I appreciate your help.

for line in `cat /tmp/l3.txt`
do
    if echo $line | grep "abc.log" ; then
        echo "X" >>/tmp/l4.txt
    else
        echo "Y" >>/tmp/l4.txt
    fi
done

Upvotes: 0

Views: 248

Answers (4)

Denio Mariz
Denio Mariz

Reputation: 1195

I think the "awk" answer above is better. However, if you really need to interact using a bash loop, you can use:

    PATTERN="abc.log"
    OUTPUTFILE=/tmp/14.txt
    INPUTFILE=/tmp/13.txt
    while read line
    do 
            grep -q "$PATTERN" <<< "$line" > /dev/null 2>&1 && echo X || echo Y
    done < $INPUTFILE >> $OUTPUTFILE

Upvotes: 0

tink
tink

Reputation: 15204

I don't understand the urge to do looping ...

awk '{if($0 ~ /abc\.log/){print "x"}else{print "y"}}' /tmp/13.txt > /tmp/14.txt

EDIT after inquiry ...

Of course, your spec wasn't overly precise, and I'm jumping to conclusions regarding your lines format ... we basically take the whole line that matched abc.log, replace everything up to the directory abc and from /log to the end of line with nothing, which leaves us with clusterX/xyz.

awk '{if($0 ~ /abc\.log/){print gensub(/.+\/abc\/(.+)\/logs/, "\\1", 1)}else{print "y"}}' /tmp/13.txt > /tmp/14.txt

Upvotes: 1

David C. Rankin
David C. Rankin

Reputation: 84561

Why worry about cat and the rest before grep, you can simply test the return of grep and append all matching lines to /tmp/14.txt or append "Y":

[ -f "/tmpfile.tmp" ] && :> /tmpfile.tmp            # test for existing tmpfile & truncate
if grep "abc.log" /tmp/13.txt >>tmpfile.tmp ; then  # write all matching lines to tmpfile
    cat tmpfile.tmp /tmp/14.txt                     # if grep matched append to /tmp/14.txt
else
    echo "Y" >> /tmp/14.txt                         # write "Y" to /tmp/14.txt
fi
rm tmpfile.tmp                                      # cleanup

Note: if you don't want the result of the grep appended to /tmp/14.txt, then just replace cat tmpfile.tmp /tmp/14.txt with echo "X" >> /tmp/14.txt and you can remove the 1st and last lines.

Upvotes: 0

MPH426
MPH426

Reputation: 49

cat /tmp/l3.txt | while read line  # read the entire line into the variable "line"
do 
  if [ -n `echo "$line" | grep "abc.log"` ]  # If there is a value "-n"
  then
    echo "X" >> /tmp/l4.txt # Echo "X" or the value of the variable "line" into l4.txt
  else
    echo "Y" >> /tmp/l4.txt # If empty echo "Y" into l4.txt      
  fi
done

While read statement will read either the entire line if only one variable is given, in this case "line" or if you have a fixed amount of fields you can specify a variable for each field, I.E. "| while read field1 field2" etc... The -n tests for if their is a value or not. -z will test if it's empty.

Upvotes: 0

Related Questions