Sponsored Content
Top Forums Shell Programming and Scripting Trim leading zeros to make field 6 characters long Post 302331653 by summer_cherry on Monday 6th of July 2009 10:10:12 PM
Old 07-06-2009
another option is to use perl unpack/unpack to process fixed-width-format string.

Code:
while(<DATA>){
	my($a,$b)=unpack('A4A6',$_);
	print $b,"\n";
}
__DATA__
0000001234
0000123456
0000234566
0000000321

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Leading zeros

How to insert leading zeros into a left-justisfied zip code? e.g. Zip code is written as 60320 which is left-justified to make it be read as 0060320. We have to move it to right-justifiable then insert 2 leading zeros into it... ;) (1 Reply)
Discussion started by: wtofu
1 Replies

2. Shell Programming and Scripting

how to retain leading zeros

Hi All, I am working with a fixed width file Forrmat. C1 Number (10,3) C2 Number (10,3) e.g. c1= 0000000100.000 c2= 0000000020.000 0000000100.0000000000020.000 I have to perform c1 - c2 . i.e. I want answer to be 0000000080.000. but I am loosing the leading zeros( only getting... (3 Replies)
Discussion started by: Manish Jha
3 Replies

3. Shell Programming and Scripting

How to trim the leading zeroes in a Currency field ?

How do I trim the leading zeroes, and (+,-) in the currency field ? I have a text file. Your bill of +00002780.96 for a/c no. 25287324 is due on 11-06. Your bill of +00422270.48 for a/c no. 28931373 is due on 11-06. I want the O/P file to be like. Your bill of 2780.96 for a/c no. 25287324... (22 Replies)
Discussion started by: Amruta Pitkar
22 Replies

4. Shell Programming and Scripting

Removing leading zeros from a variable

How do I remove or add leading zeroa from a variable. To make variable 10 characters long when adding zeros. (6 Replies)
Discussion started by: toshidas2000
6 Replies

5. Shell Programming and Scripting

Help needed in padding leading zeros

Hi all, I have file with numeric values. I need to pad each value with leading zeros such that total lenght of each value is 16. Example: cat tmp.txt 502455 50255 5026 5027 5028 Output 0000000000502455 0000000000050255 0000000000005026 0000000000005027 0000000000005028 Any... (12 Replies)
Discussion started by: jakSun8
12 Replies

6. UNIX for Dummies Questions & Answers

Triml leading zeros in unix

Hi All, How does one trim leading zero's in unix Thanks KP. (7 Replies)
Discussion started by: kingofprussia
7 Replies

7. Shell Programming and Scripting

Help with adding leading zeros to a filename

Hi i need help in adding leading zero to filenames e.g file name in my folder are 1_234sd.txt 23_234sd.txt the output i need is 001_234sd.txt 023_234sd.txt can i do this shell scripting please help (2 Replies)
Discussion started by: rsmpk
2 Replies

8. Shell Programming and Scripting

Numbers with leading zeros

Hi, i have a variable which conatins values like 00001,0003,00067,00459. I want to use the values one by one and in the same form as they are like 00001,0003,00067,00459. Also can anyone tell me how to increment those numbers by 1,keeping the format as same like 00002,0004,00068,00460.... (5 Replies)
Discussion started by: arijitsaha
5 Replies

9. Shell Programming and Scripting

Help deleting leading zeros in a file

I have a list of numbers extracted and need to delete the leading zeros from them, but when i do so, the command I am using also deletes numbers that end in Zero as well. eg 10, 20, 30, etc this is part of a larger script and the only way I can think of is to try and detect the 10,20 30 etc in... (19 Replies)
Discussion started by: kcpoole
19 Replies

10. Shell Programming and Scripting

Remove leading zeros separated by pipe

I have a below file and I wanted to remove the leading zeros in each field separated by pipe File: 01/09/2017|2017/09/06|2017/02/06|02/06/2017|02/06/2017 06:50:06 AM|2017/02/06|02/06/2017|02/07/2017 05:45:06 AM| 02/08/2017|2017/08/06|2017/09/06|02/05/2017|02/07/2017 05:40:06... (4 Replies)
Discussion started by: Joselouis
4 Replies
pack(1)                                                            User Commands                                                           pack(1)

NAME
pack, pcat, unpack - compress and expand files SYNOPSIS
pack [-f] [-] file... pcat file... unpack file... DESCRIPTION
pack The pack command attempts to store the specified files in a compressed form. Wherever possible (and useful), each input file file is replaced by a packed file file.z with the same access modes, access and modified dates, and owner as those of file. If pack is successful, file will be removed. The amount of compression obtained depends on the size of the input file and the character frequency distribution. Because a decoding tree forms the first part of each .z file, it is usually not worthwhile to pack files smaller than three blocks, unless the character frequency distribution is very skewed, which may occur with printer plots or pictures. Typically, text files are reduced to 60-75% of their original size. Load modules, which use a larger character set and have a more uniform distribution of characters, show little compression, the packed versions being about 90% of the original size. pack returns a value that is the number of files that it failed to compress. No packing will occur if: o the file appears to be already packed o the file name has more than 14 - 2 bytes o the file has links o the file is a directory o the file cannot be opened o the file is empty o no disk storage blocks will be saved by packing o a file called file.z already exists o the .z file cannot be created o an I/O error occurred during processing. The last segment of the file name must contain no more than 14 - 2 bytes to allow space for the appended .z extension. Directories cannot be compressed. pcat The pcat command does for packed files what cat(1) does for ordinary files, except that pcat cannot be used as a filter. The specified files are unpacked and written to the standard output. pcat returns the number of files it was unable to unpack. Failure may occur if: o the file cannot be opened; o the file does not appear to be the output of pack. unpack The unpack command expands files created by pack. For each file specified in the command, a search is made for a file called file.z (or just file, if file ends in .z). If this file appears to be a packed file, it is replaced by its expanded version. The new file has the .z suffix stripped from its name, and has the same access modes, access and modification dates, and owner as those of the packed file. unpack returns a value that is the number of files it was unable to unpack. Failure may occur for the same reasons that it may in pcat, as well as for the following: o a file with the ``unpacked'' name already exists; o the unpacked file cannot be created. o the filename (excluding the .z extension) has more than 14 bytes. OPTIONS
The following options are supported by pack: -f Forces packing of file. This is useful for causing an entire directory to be packed even if some of the files will not benefit. Packed files can be restored to their original form using unpack or pcat. OPERANDS
The following operands are supported: file A path name of a file to be packed, unpacked, or pcated; file can include or omit the .z suffix. - pack uses Huffman (minimum redundancy) codes on a byte-by-byte basis. If the - argument is used, an internal flag is set that causes the number of times each byte is used, its relative frequency, and the code for the byte to be printed on the standard output. Additional occurrences of - in place of file will cause the internal flag to be set and reset. USAGE
See largefile(5) for the description of the behavior of pack, pcat, and unpack when encountering files greater than or equal to 2 Gbyte ( 2 **31 bytes). EXAMPLES
Example 1: Viewing a Packed File To view a packed file named file.z use: example% pcat file.z or just: example% pcat file Example 2: Making and Unpacked Copy: To make an unpacked copy, say nnn, of a packed file named file.z (without destroying file.z) use the command: example% pcat file >nnn ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of pack, pcat, and unpack: LC_CTYPE, LC_MESSAGES, and NLSPATH. EXIT STATUS
The following exit values are returned: 0 Successful completion. >0 An error occurred. The number of files the command failed to pack/unpack is returned. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWesu | +-----------------------------+-----------------------------+ |CSI |Enabled | +-----------------------------+-----------------------------+ SEE ALSO
cat(1), compress(1), zcat(1), attributes(5), environ(5), largefile( 5) SunOS 5.10 20 Dec 1996 pack(1)
All times are GMT -4. The time now is 02:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy