Help speeding up script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help speeding up script
# 15  
Old 04-29-2015
Quote:
Code:
cat 'Tax_Provision_Sample.dat' | sort | while read p; do fn=`echo $p|cut -d~ -f2,4,3,8,9`; echo $p >> "$fn.txt"; done

You could also try:-
Code:
Orig_IFS="$IFS"
IFS=\~
sort Tax_Provision_Sample.dat | while read p1 p2 p3 p4 p5 p6 p7 p8 p9 p10
do
   echo "$p2~$p4~$p3~$p8~$p9"
done > $fn.txt
IFS="$Orig_IFS"

I'm not great with awk, so this is simpler to read, but there may well be a trade off on performance.


Just another option, although you may already have better.



Robin
# 16  
Old 04-29-2015
Robin, where does fn get set and shouldn't that be changed inside the loop?
# 17  
Old 04-29-2015
I was just using the output file name from the originally supplied code. I cannot say how it was originally set.

What do you think of the performance issues for a large input? Would my code by horribly slower? If so, then the original poster must make the decision of speed over clarity (assuming that my suggestion is clear, and I'm not sure if it is)



Regards,
Robin

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hang on, no I have totally misread the supplied attempt.

If I re-read it, the code is generating multiple output files based on the input records and writing the whole line to the appropriate file.

No, forget my suggestion, totally wrong.

I can't think of a way to remove the "open/append, write, close" operations to write to multiple files unless we force is another way by working out what files there could possibly be and then getting the records for each required output file in turn. That would just generate more headaches than it solves and for a large input file could still be quite slow.


Am am a fool Smilie



Robin

Last edited by rbatte1; 04-30-2015 at 11:07 AM..
# 18  
Old 04-29-2015
Don't we all have that from time to time Smilie

In shell the open/append, write, close operations are performed implicitly by the scope of the redirection of the file descriptor. RudiC already gave a suggestion using an array.

With you multi-variable approach:

Code:
sort Tax_Provision_Sample.dat | 
while IFS="~" read p1 p2 p3 p4 p5 p6 p7 p8 p9 p10
do
  printf "%s\n" "$p2~$p4~$p3~$p8~$p9" >> $fn.txt
done

The files would need to be empty beforehand..
# 19  
Old 04-29-2015
And where does fn get set now? Smilie
I think you mean
Code:
#!/bin/bash
declare -A AF
sort Tax_Provision_Sample.dat | 
while IFS="~" read p1 p2 p3 p4 p5 p6 p7 p8 p9 p10
do
  fn="$p2~$p4~$p3~$p8~$p9"
  if [ -z "${AF[$fn]}" ]; then
    > $fn.txt
    AF[$fn]=1
  fi
  echo "$p1~$p2~$p3~$p4~$p5~$p6~$p7~$p8~$p9~$p10" >> $fn.txt
done

I have added some bash-4 code that will empty the output files when first met.
Omit the code if you have bash-3 (and delete/empty the files before you run the script).
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 20  
Old 04-29-2015
Yes something like that Smilie . Was trying to show open/append, write, close operations in shell .. and forgot about $fn ...
# 21  
Old 04-30-2015
Oh how wonderful:-
Code:
while IFS="~" read .......

Lovely code! That certainly neatens some of the rubbish I've written. I hope you don't mind if I steal it......?



Robin
(Still learning)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with speeding up my working script to take less time - how to use more CPU usage for a script

Hello experts, we have input files with 700K lines each (one generated for every hour). and we need to convert them as below and move them to another directory once. Sample INPUT:- # cat test1 1559205600000,8474,NormalizedPortInfo,PctDiscards,0.0,Interface,BG-CTA-AX1.test.com,Vl111... (7 Replies)
Discussion started by: prvnrk
7 Replies

2. Shell Programming and Scripting

Help 'speeding' up this 'parsing' script - taking 24+ hours to run

Hi, I've written a ksh script that read a file and parse/filter/format each line. The script runs as expected but it runs for 24+ hours for a file that has 2million lines. And sometimes, the input file has 10million lines which means it can be running for more than 2 days and still not finish.... (9 Replies)
Discussion started by: newbie_01
9 Replies

3. Shell Programming and Scripting

Speeding up shell script with grep

HI Guys hoping some one can help I have two files on both containing uk phone numbers master is a file which has been collated over a few years ad currently contains around 4 million numbers new is a file which also contains 4 million number i need to split new nto two separate files... (4 Replies)
Discussion started by: dunryc
4 Replies

4. Shell Programming and Scripting

Speeding up substitutions

Hi all, I have a lookup table from which I am looking up values (from col1) and replacing them by corresponding values (from col2) in another file. lookup file a,b c,d So just replace a by b, and replace c by d. mainfile a,fvvgeggsegg,dvs a,fgeggefddddddddddg... (7 Replies)
Discussion started by: senhia83
7 Replies

5. Shell Programming and Scripting

Speeding up search and replace in a for loop

Hello, I am using sed in a for loop to replace text in a 100MB file. I have about 55,000 entries to convert in a csv file with two entries per line. The following script works to search file.txt for the first field from conversion.csv and then replace it with the second field. While it works fine,... (15 Replies)
Discussion started by: pbluescript
15 Replies

6. Shell Programming and Scripting

speeding up bash script with "while read line"

Hello everybody, I'm still slowly treading my way into bash scripting (without any prior programming experience) and hence my code is mostly what some might call "creative" if they meant well :D I have created a script that serves its purpose but it does so very slowly, since it needs to work... (4 Replies)
Discussion started by: origamisven
4 Replies

7. UNIX for Dummies Questions & Answers

Speeding/Optimizing GREP search on CSV files

Hi all, I have problem with searching hundreds of CSV files, the problem is that search is lasting too long (over 5min). Csv files are "," delimited, and have 30 fields each line, but I always grep same 4 fields - so is there a way to grep just those 4 fields to speed-up search. Example:... (11 Replies)
Discussion started by: Whit3H0rse
11 Replies

8. UNIX for Dummies Questions & Answers

Speeding up a Shell Script (find, grep and a for loop)

Hi all, I'm having some trouble with a shell script that I have put together to search our web pages for links to PDFs. The first thing I did was: ls -R | grep .pdf > /tmp/dave_pdfs.outWhich generates a list of all of the PDFs on the server. For the sake of arguement, say it looks like... (8 Replies)
Discussion started by: Dave Stockdale
8 Replies

9. Shell Programming and Scripting

Speeding up processing a file

Hi guys, I'm hoping you can help me here. I've knocked up a script that looks at a (huge) log file, and pulls from each line the hour of each transaction and how long each transaction took. The data is stored sequentially as: 07:01 blah blah blah 12456 blah 07:03 blah blah blah 234 blah... (4 Replies)
Discussion started by: dlam
4 Replies

10. Shell Programming and Scripting

speeding up the compilation on SUN Solaris environment

Dear friends, Please let me know how do I increase the speed of my compilation in SUN Solaris environment. actually I have many subfolders which contains .cc files. when I compile makefile at the root it will take much time to compile all the subfolders and generates object(.o) files. Can... (2 Replies)
Discussion started by: swamymns
2 Replies
Login or Register to Ask a Question