1833800 Members
2675 Online
110063 Solutions
New Discussion

read data in a file

 
SOLVED
Go to solution
Patrice Blanchard_2
Frequent Advisor

read data in a file

Hi,

what's the command to read the data in a file? I have a file and if a do the command:
grep Customer file
it returns:
file:Customer : 269852

How can i read the 269852 ?

Regards

PB
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: read data in a file

Shalom Patrice,

while read -r dataline
do
echo $dataline
# perhaps awk -F: '{print $2}'
# for further procesing
done < filename

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor

Re: read data in a file

You need to define your problem a little better. Do you only want to return lines that contain "Customer"? Might more than one line in the file contain "Customer"?. Is the field separator always ":"? Is the customer number always the last field in the line -- or, perhaps, always immediately follows "Customer:"?
If it ain't broke, I can fix that.
Patrice Blanchard_2
Frequent Advisor

Re: read data in a file

Let's put it this way,

i want to be able to catch the customer number in a file generated by my ERP system so that i could send an email by mailx.

So the process woulb by:
fetch the customer number in a file then read another file with that customer number to get the email address so i can send an email by mailx.

file1 would have Customer : 268852
file2 would have 268852 email@somewhere.com

then i could do a mailx to email@somewhere.com

is it feasible?

Regards

PB
A. Clay Stephenson
Acclaimed Contributor

Re: read data in a file

You are still not defining the problem precisely enough. Does your file1 contain only 1 line with customer or does it contain many different customers? Are there other lines in the file which should be ignored?

Very similar questions for line 2: Does it contain only one customer number?

It would be very helpful if you listed a dataset of file1 and file2 -- unless you already have and these are simply one line files.
If it ain't broke, I can fix that.
Patrice Blanchard_2
Frequent Advisor

Re: read data in a file

Sorry about that, i forgot that part when i did my reply.

file1 is an invoice for 1 customer so you'll have multiple lines in file1 but only 1 line with Customer : xxxxxx so you'll have something like:

invoice
blablabla
customer: 258965
blablabla
line 1 item price
...

file2 would have multiple customer number, 1 per line so you'll have something like this:
268852 email1
245896 email2
256974 email3

Rodney Hills
Honored Contributor
Solution

Re: read data in a file

Something like this should work-

cust=`awk '/^customer:/{print $2}'`
if [[ "x$cust" = "x" ]] ; then echo "no customer line..." ; exit ; fi
email=`awk '/^${cust}/{print $2}'`
if [[ "x$email" = "x" ]] ; then echo "no email for $cust" ; exit ; fi
mailx -s "subj" $email
Note I added code to exit if it can't find the data in the file

HTH

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: read data in a file

Okay, given that here is a fairly robust solution (provided that I don't make any typing booboo's):

--------------------------------------
#!/usr/bin/sh

typeset PROG=${0##*/}

if [[ ${#} -lt 2 ]]
then
echo "${PROG}: requires 2 arguments" >&2
exit 250
fi

typeset FILE1=${1}
typeset FILE2=${2}
shift 2

if [[ ! -r ${FILE1} || ! -f ${FILE1} ]]
then
echo "${PROG}: Can't open ${FILE1}" >&2
exit 248
fi

if [[ ! -r ${FILE2} || ! -f ${FILE2} ]]
then
echo "${PROG}: Can't open ${FILE2}" >&2
exit 246
fi

typeset -i STAT=255
typeset CNUM=$(awk -F ':' '/Customer/{print $NF; exit(0)}' ${FILE1})
if [[ -n "${CNUM}" ]]
then
STAT=254
typeset A=$(echo "{ if (\$1 == target) {print \$0; exit(0)}}")
typeset ADDRESS=""
typeset DUMMY=""
awk -v "target=${CNUM}" "${A}" ${FILE2} | read DUMMY ADDRESS
if [[ -n "${ADDRESS}" ]]
then
echo "Cnum: ${CNUM}"
echo "Address: ${ADDRESS}"
STAT=0
fi
fi
exit ${STAT}


--------------------------------------

At the point where I am echo'ing ${CNUM} and ${ADDRESS} is where you should build your mailx command.

Invoke the script like this:
myscript.sh /xxx/yyy/file1 /zzz/ttt/file2
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: read data in a file

PB,

You probably have a bunch of invoice files no?

So you may want to consider to read the email files once and just remember all the number to email combo's

Then loop through the input files.

Working perl example below.

hth,
Hein.


---- invoice_email.pl ------------
$email = shift @ARGV or die "Please provide number-email file";
$invoices = shift @ARGV or die "Please provide invoice file specs";
open X, "<$email" or die "Could not open number-email file $email";
while () {
chomp;
($number, $address) = split;
$addresses{$number} = $address;
}

foreach (glob $invoices) {
open X, "<$_" or die "Could not open invoice file $_";
while () {
if (/customer:\s+(\d+)/) {
$number = $1;
$address = $addresses{$number};
if ($address) {
print "system ( mailx -s $number $address ... goes here\n)";
} else {
print "No Email address found for customer: $number\n";
}
}
}
}


------- sample run ------
/tmp]$ perl x.p email "invo*"
No Email address found for customer: 258965
system ( mailx -s 268852 email1 ... goes here)
system ( mailx -s 245896 email2 ... goes here)

-------- input date -----

# grep "cust" invo*
invoice1:customer: 258965
invoice2:customer: 268852
invoice3:customer: 245896
# cat email
268852 email1
245896 email2
256974 email3
Muthukumar_5
Honored Contributor

Re: read data in a file

Do you mean that,

Get Customer number from file and grep that in file2 and get the mail Id and send invoice?

Then,

# cat file1
invoice
blablabla
customer: 258965
blablabla
line 1 item price
...
# cat file2
268852 email1
245896 email2
256974 email3
258965 email@somewhere.com
# awk '/[Cc]ustomer:/ { print $2 }' file1 | xargs -i grep {} file2 | awk '{ print $2 }'
email@somewhere.com

# mailx -s "subject" $(awk '/[Cc]ustomer:/ { print $2 }' file1 | xargs -i grep {} file2 | awk '{ print $2 }') < messagefile

hth.
Easy to suggest when don't know about the problem!
Patrice Blanchard_2
Frequent Advisor

Re: read data in a file

Thanks all for your help.

PB