Need to capture certain text from a string in a different file

Hi,

I wanted to know how i could accomplish this in a script using ksh.

Lets say there is a file called test.dat and it has a certain input like below :

.
.
 
Hi = 56
Hi = 67
.
.
 
1 record(s) selected
 

Now i need to capture the numbers after the = sign and store them in a variable.

I wrote a piece of code, but it doesnt seem to work and i dont know what i am doing wrong with this.

Here is the code :

count=0
{ while read myline;do
# process $myline
count=$(($count + 1))
word_count=`printf $myline | wc -c`
# select only rows having any data
# ignore header rows
if [ $word_count -gt 0 ] && [ $count -gt 3 ]
then
# Ignore the comments from sql statement
if [ $myline = *Hi* ] && [[ ! $myline = *record* ]]
then
TEXT=`expr substr $myline 5 2`
echo $TEXT
fi
fi
done } < test.dat

Thanks in advance

$> VAR=`grep "^Hi = " infile| cut -d" " -f3`
$> echo $VAR
56 67
1 Like

It looks too busy as well as things like "if [ $myline = *Hi* ] && [[ ! $myline = *record* ]]" do not seem to be stable or fire for your line. Maybe use case, which I like for its form and extensibility, although there are many other ksh93 ways to test strings. {} not needed. No examples of the other lines you need to worry about for some reason. All ksh, no fork/exec costs.

TEXT=
while read l
do
 case "$l" in
 ("Hi = "[0-9][0-9])
   TEXT="$TEXT ${l#*= }"
   ;;
 esac
done < test.dat
echo "${TEXT# }"
1 Like

Thank you guys for the amazing replies. They work.

I am going to go ahead and use zaxxon's way, as it kind of fits in what i actually want to do (this was just a snippet of what i actually intend to do), so special thanks to him :slight_smile:

Thank You DGPickett as well.