Visit Our UNIX and Linux User Community


Problem to read archive


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem to read archive
# 1  
Old 11-03-2009
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; 11-03-2009 at 06:23 PM.. Reason: code tags, please...
# 2  
Old 11-03-2009
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  
Old 11-04-2009
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; 11-04-2009 at 08:18 AM.. Reason: Please use code tags!
# 4  
Old 11-04-2009
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; 11-04-2009 at 08:27 AM..
# 5  
Old 11-04-2009
@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; 11-04-2009 at 09:14 AM..
# 6  
Old 11-04-2009
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  
Old 11-04-2009
Then why didn't you say so? I thought you specifically wanted a shell scriptSmilie. Try this. Smilie
Code:
awk '{print>substr($0,38,2)"_file.txt"}' file.txt

You can make it even faster if you use mawk.

Previous Thread | Next Thread
Test Your Knowledge in Computers #501
Difficulty: Easy
If a piece of code adds two numbers together and stores the result, there should be a comment that explains the process in the source code.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to archive logs and sftp to another archive server

Requirement: Under fuse application we have placeholders called containers; Every container has their logs under: <container1>/data/log/fuse.log <container1>/data/log/fuse.log.1 <container1>/data/log/fuse.log.XX <container2>/data/log/fuse.log... (6 Replies)
Discussion started by: Arjun Goswami
6 Replies

2. Solaris

Boot-archive Problem

I disabled the boot-archive service by using #svcadm disable svc:/system/boot-archive:default then i rebooted my system but i am unable to boot. It throws the following errors CONSOLE LOGIN SERVICE(S) CANNOT RUN then it automatically asked me for the maintenance mode passwd. i logged... (3 Replies)
Discussion started by: dinu
3 Replies

3. Shell Programming and Scripting

Extracting from archive | compressing to new archive

Hi there, I have one huge archive (it's a system image). I need sometime to create smaller archives with only one or two file from my big archive. So I'm looking for a command that extracts files from an archive and pipe them to another one. I tried the following : tar -xzOf oldarchive.tgz... (5 Replies)
Discussion started by: chebarbudo
5 Replies

4. Shell Programming and Scripting

Archive::Tar problem

Hello, I have a problem using Archive::Tar. it seem very trivial but i cannot get it work. First I have a list of files I grab from a directory. Then I create a tar archive and write the files into the archive. everything works great, except that I cannot properly extract the files. What... (0 Replies)
Discussion started by: amcrisan
0 Replies

5. Programming

read file from tar.gz archive

I want to write a c-program which reads a textfile from a tar.gz archive. How can I do it? (9 Replies)
Discussion started by: krylin
9 Replies

6. Shell Programming and Scripting

Read specific file from a zip archive without extracting

Hi All, I would like to extract specific file from a zip archive. I have a zip archive "sample.zip". sample.zip contains few text files and images... text1.txt, text2.txt, pic.jpg etc... I need to read specific file "text2.txt" from "sample.zip" WITHOUT EXTRACTING the zip file. ... (4 Replies)
Discussion started by: sridharg
4 Replies

7. Solaris

problem in creating flash archive

Dear all I am in a problem. I have created a master server on which I have install a Solaris 10 OS as well as Oracle 10g with some additional solaris packages. Now I want to create a flash archive of this server and install that flash archive on another server, so that the new server will have... (6 Replies)
Discussion started by: girish.batra
6 Replies

8. Shell Programming and Scripting

read list of filenames from text file, archive, and remove

I posted a week ago regarding this scripting question, but I need to revisit and have a few more questions answered.. User cfajohnson was extremely helpful with the archive script, but clarification on my part is needed to help steer the answer in a direction that works in this particular... (5 Replies)
Discussion started by: fxvisions
5 Replies

9. Shell Programming and Scripting

Read from file then purge or archive.

Hi All, I have a root directory /tmp and I want to purge files or archive files in its subsequent subfolders.I listed the path of files I want to purge(archive) and the #of days. (purge) DAYS PATH 7 /tmp/arsenal/* 5 /tmp/chelsea/* (archive? the same as above but different folders... (15 Replies)
Discussion started by: kayarsenal
15 Replies

Featured Tech Videos