![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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... |
|
||||
|
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
|
|
||||
|
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! |
|
||||
|
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.. |
|
||||
|
@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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|