Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UNIX command help needed

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi guys

I have 120+ price/ holdings files, which have been unloaded from my in house software. I
now need to strip out the top 12 lines from each file (the price section
only), and copy these chunks of text into one big file. Copy & pasting in a
text editor would do it, but it'll take all afternoon.

Does anyone know of a quicker way of doing this (with a unix command within a
batch file maybe)?

Thanks in advance

Steve
 
Do the names of these files have something in common? Are all files in a separate directory of their own? Do you need to process the files in any particular order?

Something like this should do the trick ...

for i in $(ls price_file_pattern)
do
head -12 $i >> main_file
done

Greg.
 
Hi,
Don't forget the CUT to move only the price section only.

for i in $(ls price_file_pattern)
do
head -12 $i | cut -d' ' -f1 >> main_file
done



this Cut assumes the delimiter between the price and the holding is a space. if it is a comma change it to

-d','

and so on for other delimiters.

Now if you want to sort the files so you can get the TOP 12 items in each file add sort before the head.


for i in $(ls price_file_pattern)
do
sort $i | head -12 | cut -d' ' -f1 >> main_file
done




 
And finally, in order to stop opening and
closing your output file every time ( which
definitely effects performance for larger
tasks ), put the redirection on the outside
of the loop ( available in ksh:

#!/bin/ksh

for i in $(ls price_file_pattern)
do
sort $i | head -12 | cut -d' ' -f1
done > main_file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top