Operating System - HP-UX
1752440 Members
5875 Online
108788 Solutions
New Discussion юеВ

Re: 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 16
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