The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read file from tar.gz archive krylin High Level Programming 9 05-20-2009 05:51 PM
Read specific file from a zip archive without extracting sridharg Shell Programming and Scripting 4 12-02-2008 11:36 AM
read list of filenames from text file, archive, and remove fxvisions Shell Programming and Scripting 5 03-20-2007 09:56 PM
Read from file then purge or archive. kayarsenal Shell Programming and Scripting 15 08-10-2006 09:24 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
cewa67 cewa67 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 4
Problem to read archive

Dear all,

I have this archive: cat file.txt

Code:
archive test  02  sequence 03        02length     52
archive test  02  sequence 04        02length     52
archive test  02  sequence 05        02length     52
teste arquivo 06 sequencia 08        06  length     54
teste arquivo 06 sequencia 09        06  length     54
teste arquivo 08 sequencia 01        08                                    length     88
teste arquivo 09 sequencia 01        09                           length     79

I have this shell....
----------------------------------

Code:
#!/Bin/ksh
dir_work=/aplic/tmp
arquivo=file.txt
cd ${dir_work}
cat $arquivo | while read registro
do
   campo=`echo $registro | cut -c38-39`
   echo $registro >>${campo}_${arquivo} 
done

-----------------------------------
I have a problem when I try to separate in 4 archives (Types 02, 06, 08 and 09).
I hope this results:

Code:
02_file.txt with 3 records
06_file.txt with 2 records
08_file.txt with 1 record
09_file.txt with 1 record.

Help me please.

Last edited by pludi; 4 Weeks Ago at 06:23 PM.. Reason: code tags, please...
  #2 (permalink)  
Old 4 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 761
One problem with your script, is that it should be bin, not Bin. Also you have to use double quotes around $registro to maintain appropriate spacing.

Code:
#!/bin/ksh
dir_work=/aplic/tmp
arquivo=file.txt
cd ${dir_work}
cat $arquivo | while read registro
do
   campo=`echo "$registro" | cut -c38-39`
   echo $registro >>${campo}_${arquivo} 
done

If you want to retain the original spacing of the input file in the output files you'd also have to use double quotes:

Code:
echo "$registro" >>${campo}_${arquivo}

You can easily check what your script is doing by temporarily using

Code:
#!/bin/ksh -x

An alternative way to do it would be:

Code:
#!/bin/ksh
dir_work=/aplic/tmp
arquivo=file.txt
cd ${dir_work}
while read registro; do
   campo=${registro:37:2}
   echo $registro >>${campo}_${arquivo}
done<$arquivo

  #3 (permalink)  
Old 4 Weeks Ago
cewa67 cewa67 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 4
Thanks.... It's solved part of my problem...
I have a header and trailer.....
Header

Code:
                                     00                                   02112009

Trailer

Code:
                                     99     00000061

The shell included header in Type 02 (didn't consider the spaces on the left side) and I lost the Trailer.....
The Trailer contains total amount of register type.

Last edited by Franklin52; 4 Weeks Ago at 08:18 AM.. Reason: Please use code tags!
  #4 (permalink)  
Old 4 Weeks Ago
rdcwayx rdcwayx is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 290

Code:
$ cat file.txt |awk '{print $3}'  |sort |uniq -c |sort  -k2 |awk '{print $2"_file.txt with",$1, "records"}'
02_file.txt with 3 records
06_file.txt with 2 records
08_file.txt with 1 records
09_file.txt with 1 records


Code:
$ awk '{a[$3]+=1} END {for (i in a) print i"_file.txt with",a[i],"records" }' file.txt  |sort
02_file.txt with 3 records
06_file.txt with 2 records
08_file.txt with 1 records
09_file.txt with 1 records


Last edited by rdcwayx; 4 Weeks Ago at 08:27 AM..
  #5 (permalink)  
Old 4 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 761
@cewa67: Try using IFS= read -r instead of read

Code:
while IFS= read -r registro


Code:
grep . *_*
00_file.txt:00 02112009
02_file.txt:archive test 02 sequence 03 02length 52
02_file.txt:archive test 02 sequence 04 02length 52
02_file.txt:archive test 02 sequence 05 02length 52
06_file.txt:teste arquivo 06 sequencia 08 06 length 54
06_file.txt:teste arquivo 06 sequencia 09 06 length 54
08_file.txt:teste arquivo 08 sequencia 01 08 length 88
09_file.txt:teste arquivo 09 sequencia 01 09 length 79
99_file.txt:99 00000061

If I put double quotes around $registro when writing to the output files, to preserve original spacing I get:

Code:
00_file.txt:                                     00                                   02112009
02_file.txt:archive test  02  sequence 03        02length     52
02_file.txt:archive test  02  sequence 04        02length     52
02_file.txt:archive test  02  sequence 05        02length     52
06_file.txt:teste arquivo 06 sequencia 08        06  length     54
06_file.txt:teste arquivo 06 sequencia 09        06  length     54
08_file.txt:teste arquivo 08 sequencia 01        08                                    length     88
09_file.txt:teste arquivo 09 sequencia 01        09                           length     79
99_file.txt:                                     99     00000061


Last edited by Scrutinizer; 4 Weeks Ago at 09:14 AM..
  #6 (permalink)  
Old 4 Weeks Ago
cewa67 cewa67 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 4
I use IFS= read -r instead of read and I get the results.. Thanks.
But I have other problem... the performance...
Processed 94.000 records in 3 minutes but I have 93.000.000 records.
How I solve this?
  #7 (permalink)  
Old 4 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 761
Then why didn't you say so? I thought you specifically wanted a shell script. Try this.

Code:
awk '{print>substr($0,38,2)"_file.txt"}' file.txt

You can make it even faster if you use mawk.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0