Parse file and copy value to variable in sh script.

Hi,

I need to parse a simple text file like below and store the word that starts with BR* to a variable say $BRno. I need to do this in sh script.

NOTE: the length of the numbers following BR is not constant (eg: it could be BR1234 or BR22233). And there is only 1 BRxxxxx in a file at a given time.

.txt file:

BR276828
Apple
Orange
CHERRY
test.txt

Gurus, Please help as soon as you can !

Thanks !

*************************
This is not a homework. I am working on a few automation scripts at work. My manager just added a few simple requirements at the last minute today. This is one of them. And I need to roll-out the scripts tomorrow. I need to get a return value from a perl script to a sh script(calling the perl script from here). So I was tyring this method for that: copying the output of the perl script to a file, and trying to parse that file from the sh script to get the BRXXX to a variable that I will print out.

This is all probably confusing, that's why I simply just asked what I need without the background. Sorry about that !

Thanks fo any help !

********************

BRno=$(grep '^BR' filename)
echo $BRno
1 Like

Thank you so much. it worked !

var=`sed -n '/^BR/{p;q}' file.in`
echo $var

---------- Post updated at 08:46 PM ---------- Previous update was at 08:44 PM ----------

var=`sed -n '/^BR/{p;q}' file.in`
echo $var

1 Like

After I find the string, I need to remove 'BR' from it, and just store the numbers following 'BR'

The following worked for .pl script. How can I change it to work in shell script ?

$BR = substr($BRNo, 2, 7)

If BRno is BR123457

Something like: (easy to expnad more settings, not only BR)

cat file | while read line
do
        BR=""
        case "$line" in
                BR*) BR=${line:2} ;;
        esac
        [ "$BR" != "" ] && echo "BR $BR"
done
1 Like

Thanks will try this.