Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I have a new assignment which I need to finish it soon, but it doesn't seem to easier for me. My requirements are given below.
In a directory /data/xfer/prod/ocs/staging, we have couple of files with the extension .orders. If I do a "ls -al *.orders", the output will be like the one given below.
-rw-r--r-- 1 gtw page 18150 Mar 10 09:11 13557915579100.orders
-rw-r--r-- 1 gtw page 6148 Mar 10 12:07 13558442610700.orders
-rw-r--r-- 1 gtw page 5680 Mar 10 12:43 13558549388150.orders
-rw-r--r-- 1 gtw page 5863 Mar 10 13:31 13558694610750.orders
-rw-r--r-- 1 gtw page 6215 Mar 10 13:36 13558710838900.orders
-rw-rw-r-- 1 gtw page 2966 Mar 12 18:28 13568224687350.ordersEach of these files are XMLS and they contain the web order # in between the tags <web_ordno> and </web_ordno>. If I do a grep on /web_ordno, the output is as given below.
13557915579100.orders: <web_ordno>38539413</web_ordno>
13558442610700.orders: <web_ordno>110001603</web_ordno>
13558442610700.orders: <web_ordno>110001603</web_ordno>Now, my requirement is generate a report with the file name, the web order # and the timestamp of the .orders file.
A Sample output is shown below.
Date/Time Staged File Web Order
========= =========== =========
Mar 10 09:11 13557915579100.orders 38539413Can some one help me?
Thanks,
Balaji.

The first thing to decide is the overall approach you will use, and whether you will use awk or sed or whatever. Your script needs to process multiple files of a given pattern, so that will have an impact on your solution, as opposed to a script that has a single known input filename.
Regarding how to find and process the files, below are two approaches, and of course there are more, such as using the find command.
This first approach uses a for-loop. Each loop will "process" one file. My code just echos the filename, but instead of echo, you would have a command or commands that would print a report using that file as input.
stagingdir=/data/xfer/prod/ocs/staging
for fname in $stagingdir/*.orders
do
echo $fname
done
The second approach uses awk to process each file. Most unix commands have this ability to process a list of files like this. This awk command processes each line of each file, and specifically, it prints the current filename and record number within that file. But of course, you would want awk to locate the desired records and print a report.awk '{print FILENAME ": Record " FNR}' $stagingdir/*.orders

Thanks for your reply.
I am able to complete the script. Here is my code.
=============================================
for stagedFile in $_stagingDir/*.orders
do
ocsOrder=`grep "/web_ordno" $stagedFile | sort -u | awk -F">" '{print $2}' | awk -F"<" '{print $1}'`
ls -al $stagedFile | awk -F" " '{ print $6,$7,$8 }' > /out/web_orders.txt
read fileTime < /out/web_orders.txt
echo $fileTime " " $ocsOrder " " $stagedFile >> /out/staged_orders.txt
done
=============================================Not sure if this is the cleanest way, but I am able to acheive my results.

good effort, however, IMO, its too messy. You can use just one awk process to do your stuff. There is no need to use too much unix pipes.
ls -1l *orders| awk 'BEGIN{
printf ( "%15s%15s%15s\n", "Date/Time","Staged File", "Web Order" )
}
{
while( (getline line < $NF) > 0 ) {
if ( line ~ /<web_ordno>/ ) {
gsub(/<web_ordno>|<\/web_ordno>/,"",line)
order=line
}
}
if ( order ) {
printf ( "%15s%15s%15s\n",$6" "$7" "$8,$NF, order)
}
}'
output:
# ./test.sh
Date/Time Staged File Web Order
Apr 2 18:42 test.orders 38539413
Apr 2 18:42 test1.orders 38539323

Fortunately or unfortunately, my code is already promoted to roduction.
Anyway, thanks for your help. I'll definitely need to learn awk extensively. Is there any good ebook or link from where I can learn awk.
Thanks,
Balaji.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |