cancel
Showing results for 
Search instead for 
Did you mean: 

Seperating data from a file

pk1974
Occasional Contributor

Seperating data from a file

Hi

I attaching a file,
in that file every record starts with PURCHASE ORDER NUMBER,
so I want to seperate the every record starts with PURCHASE ORDER NUMBER and create a new file for every one records,

how can I used grep command

please advise me

thanks

16 REPLIES
Pete Randall
Outstanding Contributor

Re: Seperating data from a file

I'm sorry but that doesn't make any sense at all. On the one hand you say that "every record starts with PURCHASE ORDER NUMBER" but then you say you want to separate "the every record starts with PURCHASE ORDER NUMBER". If they all start with PURCHASE ORDER NUMBER, what's left to separate?


Pete

Pete
Autocross.US
Trusted Contributor

Re: Seperating data from a file

Wow,

i bet Verizon won't be too pleased to see this kind of info posted on a public forum.
I drive way too fast to worry about calories.
PK_1975
Frequent Advisor

Re: Seperating data from a file

Hi

Please find the attachment

I want to generate new files for every record in the attached file

In the attached file

for example in the attached file

one record is in italic and bold

and other record is plane

I want to create 2 files, one file for Italic and bold record and second file for the plane text record
Hein van den Heuvel
Honored Contributor

Re: Seperating data from a file

You can not use a grep command to do this.
But AWK ro PERL woudl be happy to.
For example, if you want to create files called S0155807.tmp where S0155807 is the
PURCHASE_ORDER_NO then you coudl simply use:

awk '/^PURCHASE_ORDER_NO:/{file=$2 ".tmp"} file { print >> file}' your-file

So there are 2 lines in this 'script':

/^PURCHASE_ORDER_NO:/{file=$2 ".tmp"}
That says, IF you see a line STARTING with PU... THEN set variable 'file' to the concatenation of field 2 on the line with the string ".tmp"

Next line:

file { print >> file}

That says... IF variable 'file' is set THEN APPEND the input line ($0) to the fiel with the name in variable 'file'.

Enjoy!

and... uh... let's just pretend those were mock-up names and phone numbers you posted there in a public forum huh? This time.
And there won't be a next time right?

Cheers,
Hein.

Hein van den Heuvel
Honored Contributor

Re: Seperating data from a file

So... did the awk solution work for you? Hein.
PK_1975
Frequent Advisor

Re: Seperating data from a file

HI

awk command is working fine when I use from command line,

when I use the command in the script and specifying the file path it is not working

eg

awk '/^PURCHASE_ORDER_NO:/{file=$2 ".tmp"} file { print >> file}' /home/abc/abc.txt


could you please advise me who to specify the file path
Fredrik.eriksson
Valued Contributor

Re: Seperating data from a file

I think I understands what he wants to do.

He wants to seperate 1 complete record to a file and so on. So that each new file contains all the information of that purchase record.

Something like this might do the trick :P but as I don't have access to a shell atm this isn't tested.
#!/bin/bash
# Set arguments to variables
# Which file to parse from
input_file=$1
# The standard which output files will be named in, ex. order-S0155806
namestd=$2

# Change IFS to handle newlines instead of space
IFS="\n"
for i in $(cat $input_file); do
# Just to check if we are going to start a new file or not
echo $i | grep "PURCHASE_ORDER_NO:" &> /dev/null
if [ $? -eq 0 ]; then
# Get the order id number to seperate the files from each other
orderid=$(echo $i | sed -e "s/^PURCHASE_ORDER_NO: *\([A-Za-z0-9]\) *$/\1/")
# Create the new filename
output_file="$namestd-$orderid"
fi
# Write the output to the file.
echo $i >> $output_file
done

(And yes, it's a slow day and I'm bored :P)

Best regards
Fredrik Eriksson
Hein van den Heuvel
Honored Contributor

Re: Seperating data from a file

>> it is not working

Hmmm...
Errors?
'Nothing happens'?
Files create in places you did not expect?

>> awk '/^PURCHASE_ORDER_NO:/{file=$2 ".tmp"} file { print >> file}' /home/abc/abc.txt

Apparently you needed to epxlicitly specify the input.
You may need to expllicitly specify the output.

To do this you may want to change:

file=$2 ".tmp"

to

file= "/home/abc/" $2 ".tmp"

Good luck,
Hein.
PK_1975
Frequent Advisor

Re: Seperating data from a file

Hi

As per ur suggestion I used the command in the script, the multiple files creating,
I want to pick each file and send email, one email for one file, if it creates 100 files, it should send 100 emails
could you please tell me how to process

Here I am using pick one file and send email,
how can I handle multiple files to send email, with file name starts with $2".tmp"
if test -s "${input_file:?}"
then
elm -s "${email_subject:?}" \
"${email_recipients[@]:?}" < "${input_file:?}"

Thanks
Dennis Handly
Acclaimed Contributor

Re: Seperating data from a file

>one email for one file, if it creates 100 files, it should send 100 emails. could you please tell me how to process?

for file in what-ever; do
mailx -s "${email_subject:?}" "${email_recipients[@]:?}" < $file
done
PK_1975
Frequent Advisor

Re: Seperating data from a file

Hi

u specified as

"for file in what-ever; do"

what-ever means?
I have to specify anything, could you please example

thanks
Dennis Handly
Acclaimed Contributor

Re: Seperating data from a file

>what-ever means? I have to specify anything, could you please example

You need to mention how you get those 100 files. If you do them one by one, then you can remove that for loop and just use $2.tmp.

If you are going to do them all at once, you will need to get a list of those files. Either from ls(1) or from a file. You need to be more specific where that mail command goes after you create the file(s).
TTr
Honored Contributor

Re: Seperating data from a file

Who is the email receipient for each PO file? Is it the person in the "Order Contact: " line or the person in the last "NOTES" line as in "NOTES: XXX FIRST LAST 856-xxx-yyyy"?
In that case you need to read each PO file and extract the name and then convert that to an email address based on whatever format you use.
Hein van den Heuvel
Honored Contributor

Re: Seperating data from a file

I guess we should have asked the old but valueable:
'WHAT PROBLEM ARE YOU REALLY TRYING TO SOLVE'.

WHY stick each order in a unique file, only to Email thme out? a SHELL or PERL script could just keep all data in memory and call mail directly from a sub-process feeding it the lines without ever needing a temp file to hold an order.

The AWK script could easily detect the new order condition END as well as the already present /^PURCHASE_ORDER_NO/ test, and then just do the Email of the past order right there and then, re-suing the same file (name) for the next order, renaming failed sends to a file to remain on the box with the order number in the file. Or in sticking with the unique names, perhaps delete succesfully send temp files.

HOW will you determine the "to:" Email address? Can that be integrated in the scripts? Perhaps a helper file with (phone) number to Email mapping?

Good luck!
Hein.


PK_1975
Frequent Advisor

Re: Seperating data from a file

Hi

Now I am able to send individual email for every individual file by the using the below code

for file in `ls /home/abc/*.tmp`
do
if test -s "${file:?}"
then
elm -s "${email_subject:?}" "${email_recipients[@]:?}" < $file
fi
done

Now I want to use only file name from the /home/abc directory and place that filename in the email subject for every email

the email subject should be like this "welcome abc.txt"

how can I place name of the file in the code could you please help me

Thanks
TTr
Honored Contributor

Re: Seperating data from a file

Before the elm line in your script, calculate the filename by using the basename command
filename=`basename $file`
elm -s "$filename" "${email_recipients[@]:?}" < $file
You can append or prepend any other text in the filename variable