Script Optimization - large delimited file, for loop with many greps


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script Optimization - large delimited file, for loop with many greps
# 15  
Old 04-26-2011
Could you post what that section actually looks like, then? I'm tempted to say 'screw it' and write a filter in C that translates this dog's breakfast into sanely-separated newline-delimited data, but I'm not sure you have a compiler.

Last edited by Corona688; 04-26-2011 at 09:35 PM..
# 16  
Old 04-26-2011
Here are just a few lines and for simplicity sake they are all the same record type. I can attach a file if it makes things clearer.

From this ...
Code:
        ISA^00^          ^00^          ^ZZ^ABCD TPU       ^ZZ^FGI  TPM       ^110423^1817^U^00401^000139971^0^P^@*GS^FA^ABCD TPU^FGI  TPM^110423^1817^139971^X^002000*ST^997^0001*AK1^PO^916*AK2^830^000053942*AK5^A*AK9^A^1^1^1*SE^6^0001*GE^1^139971*IEA^1^000139971*                                                                 ISA¼00¼          ¼00¼          ¼ZZ¼ABCD TPU       ¼ZZ¼FGI  TPM       ¼110423¼1817¼U¼00401¼000139972¼0¼P¼@|GS¼FA¼ABCD TPU¼FGI  TPM¼110423¼1817¼139972¼X¼002000|ST¼997¼0001|AK1¼PO¼916|AK2¼830¼000053943|AK5¼A|AK9¼A¼1¼1¼1|SE¼6¼0001|GE¼1¼139972|IEA¼1¼000139972|                                                                 ISA|00|          |00|          |ZZ|ABCD TPU       |ZZ|FGI  TPM       |110423|1817|U|00401|000139973|0|P|@*GS|FA|ABCD TPU|FGI  TPM|110423|1817|139973|X|002000*ST|997|0001*AK1|PO|916*AK2|830|000053944*AK5|A*AK9|A|1|1|1*SE|6|0001*GE|1|139973*IEA|1|000139973*

To this ...
Code:
ISA^00^          ^00^          ^ZZ^ABCD TPU       ^ZZ^FGI  TPM       ^110423^1817^U^00401^000139971^0^P^@
GS^FA^ABCD TPU^FGI{TPM^110423^1817^139971^X^002000
ST^997^0001
AK1^PO^916
AK2^830^000053942
AK5^A
AK9^A^1^1^1
SE^6^0001
GE^1^139971
IEA^1^000139971
ISA¼00¼          ¼00¼          ¼ZZ¼ABCD TPU       ¼ZZ¼FGI  TPM       ¼110423¼1817¼U¼00401¼000139972¼0¼P¼@
GS¼FA¼ABCD-TPU¼FGI  TPM¼110423¼1817¼139972¼X¼002000
ST¼997¼0001
AK1¼PO¼916
AK2¼830¼000053943
AK5¼A
AK9¼A¼1¼1¼1
SE¼6¼0001
GE¼1¼139972
IEA¼1¼000139972
ISA|00|          |00|          |ZZ|ABCD TPU       |ZZ|FGI  TPM       |110423|1817|U|00401|000139973|0|P|@
GS|FA|ABCD,TPU|FGI  TPM|110423|1817|139973|X|002000
ST|997|0001
AK1|PO|916
AK2|830|000053944
AK5|A
AK9|A|1|1|1

---------- Post updated at 05:56 PM ---------- Previous update was at 05:50 PM ----------

It can be beastly ... there are commercial applications that do this kind of thing but politics and red tape ... it's must faster for me to write a script

although my skills to do so need work ... again, I can't say it enough, thanks for your help
# 17  
Old 04-26-2011
Yes, but do you have a C compiler? You're pretty much going to need to parse this character-by-character to get the delimiters right, and that's not efficient in any language but C.
# 18  
Old 04-27-2011
no Corona688, I don't have a C compiler Smilie

If it helps the token seperator (pos 4) and the line terminator (pos 107) are always in the same position, in the first line (ISA) ... the ISA line is fixed length.

This is how I'd do it with my inefficient command-line type way ... however sed doesn't work well with some delimiters

Code:
#!/usr/bin/ksh
perl -p0e 's/[\W_]ISA[\W_]/\n$&/g' orig_file>all_isas
csplit -ks -n 6 -f isa_each all_isas /^ISA/ {100000}
ls -1 isa_each>filelist
 
for file in `ls -1 isa_each*`;
do
delimseg=`grep "^ISA" newisa_1|cut -c4`
delimline=`grep "^ISA" newisa_1|cut -c107`
sed 's/\$delimseg/~/g' $file | sed 's/\$delimline/x0D/g' >$file.cleaned
done
 
and then I'd run your awk script

I was able to use your code to work with the first scenario but I don't understand awk enough to make it work in this scenario
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Need Optimization shell/awk script to aggreagte (sum) for all the columns of Huge data file

Optimization shell/awk script to aggregate (sum) for all the columns of Huge data file File delimiter "|" Need to have Sum of all columns, with column number : aggregation (summation) for each column File not having the header Like below - Column 1 "Total Column 2 : "Total ... ...... (2 Replies)
Discussion started by: kartikirans
2 Replies

2. UNIX for Advanced & Expert Users

Need optimized awk/perl/shell to give the statistics for the Large delimited file

I have a file size is around 24 G with 14 columns, delimiter with "|" My requirement- can anyone provide me the fastest and best to get the below results Number of records of the file First column and second Column- Unique counts Thanks for your time Karti ------ Post updated at... (3 Replies)
Discussion started by: kartikirans
3 Replies

3. Shell Programming and Scripting

Tab Delimited file in loop

Hi, I have requirement to create tab delimited file with values coming from variables. File will contain only two columns separated by tab. Header will be added once. Values will be keep adding upon the script run. If values already exists then values will be replaced. I have done so... (1 Reply)
Discussion started by: sukhdip
1 Replies

4. Shell Programming and Scripting

Need a script to convert comma delimited files to semi colon delimited

Hi All, I need a unix script to convert .csv files to .skv files (changing a comma delimited file to a semi colon delimited file). I am a unix newbie and so don't know where to start. The script will be scheduled using cron and needs to convert each .csv file in a particular folder to a .skv... (4 Replies)
Discussion started by: CarpKing
4 Replies

5. Shell Programming and Scripting

Removing dupes within 2 delimited areas in a large dictionary file

Hello, I have a very large dictionary file which is in text format and which contains a large number of sub-sections. Each sub-section starts with the following header : #DATA #VALID 1 and ends with a footer as shown below #END The data between the Header and the Footer consists of... (6 Replies)
Discussion started by: gimley
6 Replies

6. Shell Programming and Scripting

help with a shell script that greps an error from the logs

Hello everyone. I wrote the following script but the second part is not excecuting. It is not sending the notification by email if the error occurs. the send mail is working so i think the errorr should be in the if statement LOGDIR=/logs/out LOG=`date "+%Y%m%d"`.LOG-FILE.out #the log file ... (11 Replies)
Discussion started by: adak2010
11 Replies

7. Shell Programming and Scripting

Extracting a portion of data from a very large tab delimited text file

Hi All I wanted to know how to effectively delete some columns in a large tab delimited file. I have a file that contains 5 columns and almost 100,000 rows 3456 f g t t 3456 g h 456 f h 4567 f g h z 345 f g 567 h j k lThis is a very large data file and tab delimited. I need... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

8. Shell Programming and Scripting

Large pipe delimited file that I need to add CR/LF every n fields

I have a large flat file with variable length fields that are pipe delimited. The file has no new line or CR/LF characters to indicate a new record. I need to parse the file and after some number of fields, I need to insert a CR/LF to start the next record. Input file ... (2 Replies)
Discussion started by: clintrpeterson
2 Replies

9. UNIX for Dummies Questions & Answers

Command that creates file and also greps that file?

I have a command that does something and then creates a log file (importlog.xml). I then want to grep that newly created log (importlog.xml) file for a certain word (success). I then want to write that grep result to a new file (success.log). So far I can run the command which creates the... (2 Replies)
Discussion started by: Sepia
2 Replies

10. Shell Programming and Scripting

Directory sizes loop optimization

I have the following script: #!/usr/bin/ksh export MDIR=/datafiles NAME=$1 SERVER=$2 DIRECTORY=$3 DATABASE=$4 ID=$5 export dirlist=`/usr/bin/ssh -q $ID@$SERVER find $DIRECTORY -type d -print` for dir in $dirlist do SIZE=`</dev/null /usr/bin/ssh -q $ID@$SERVER du -ks $dir` echo... (6 Replies)
Discussion started by: la_womn
6 Replies
Login or Register to Ask a Question