Sponsored Content
Full Discussion: Padding a csv value with 0's
Top Forums Shell Programming and Scripting Padding a csv value with 0's Post 302848043 by shamrock on Wednesday 28th of August 2013 10:59:07 AM
Old 08-28-2013
How do you want field 21 sorted...lexically or numerically...and post a sample of how the output looks like because from your post there seems to be no difference in the input and output...and you can try the sort below which will divvy up field 21 into 2 parts i.e. the first 3 characters are sorted lexically and the rest are sorted numerically...is this something you are looking for...
Code:
sort -t, -k21.1,21.3 -k21.4,21n file

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Padding issues

Hello, Structure padding & structure size are different on Compaq & HP UNIX. When structures are transfered via netfork from Compaq to HP will this be a problem? If yes, what can be the solution? Thanks, shilpa (2 Replies)
Discussion started by: informshilpa
2 Replies

2. Programming

Zero Padding to a string

I am writing a C program which a part of it needs to padding zero in front of a string. The program will get a sting from an ASCII file which the maxium length of this string is 5 char long. The string can sometimes less the 5 char long. In order to make it with the same length '0's are being... (3 Replies)
Discussion started by: Wing m. Cheng
3 Replies

3. UNIX for Dummies Questions & Answers

Padding

Hi Can anyone tell me how to pad zeroes on the left side to a numeric string in unix shell scripting Your answer is very much appreciated Thanks Vijay (2 Replies)
Discussion started by: vijaygopalsk
2 Replies

4. Programming

Byte Padding

Hi, Can someone explain what is byte padding? For ex: struct emp{ char s; int b; char s1; int b1; long b3; char s3; } What will be the size of this structure? Thanks (6 Replies)
Discussion started by: naan
6 Replies

5. Shell Programming and Scripting

Padding in Unix

I have a file with different character counts on each line how do i make it with unique character counts. example: 1st line : ABCD 011 XYZ 0000 YYYY BBB TEADINGDA 2nd line: ABCD 011 xys 0010 YYYY BBB TEAD 3rd line : ABCD 022 YXU 000 UUU BBB TE 1st line is 43... (3 Replies)
Discussion started by: rudoraj
3 Replies

6. Shell Programming and Scripting

CSV formatting with prefixing, appending and padding field

Hi I have a very large csv file with some hundreds of thousands of rows of data. The data is in the following format: Up to four alpha numeric characters for the first word. This is either set as 2 characters followed by 2 spaces, or as a single 4character word. This is then followed by an 8... (7 Replies)
Discussion started by: meself
7 Replies

7. UNIX for Dummies Questions & Answers

Zero padding dates

I have a file with records containing dates like: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|2007-6-6|MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|2003-11-6|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| How can I get the date fields in each of my records to be... (1 Reply)
Discussion started by: ChicagoBlues
1 Replies

8. Shell Programming and Scripting

Padding with zeros.

Hi Friends, I would like to left pad with "0's" on first column say (width six) I have a large file with the format: FILE: 1: ALFRED 84378 NY 8385: JAMES 88385 FL 323: SMITH 00850 TX My output needs to be like: 000001: ALFRED 84378 NY 008385: JAMES 88385 FL 000323: SMITH... (10 Replies)
Discussion started by: sbasetty
10 Replies

9. Shell Programming and Scripting

Padding leading zero

hi All i am new to linux... source txt .. 281-BUM-5M BUM-5M 0 0 282-BUM-5M BUM-5M 0 0 83-BUM-5M BUM-5M 0 0 is it possible to use bash script to convert to (remove the "-" and fill up to 4 digit" ? 0281 BUM-5M BUM-5M 0 0 0282 BUM-5M BUM-5M 0 0 0083 BUM-5M BUM-5M 0 0 thanks a ... (5 Replies)
Discussion started by: samoptimus
5 Replies

10. Shell Programming and Scripting

Sort, sed, and zero padding date column csv bash scripting

Hello people, I am having problem to sort, sed and zero padding of column in csv file. 7th column only. Input of csv file: 1,2,3,4,5,6,4/1/2010 12:00 AM,8 1,2,3,4,5,6,3/11/2010 9:39 AM,8 1,2,3,4,5,6,5/12/2011 3:43 PM,8 1,2,3,4,5,6,12/20/2009 7:23 PM,8 Output:... (5 Replies)
Discussion started by: sean1357
5 Replies
Sort::Naturally(3pm)					User Contributed Perl Documentation				      Sort::Naturally(3pm)

NAME
Sort::Naturally -- sort lexically, but sort numeral parts numerically SYNOPSIS
@them = nsort(qw( foo12a foo12z foo13a foo 14 9x foo12 fooa foolio Foolio Foo12a )); print join(' ', @them), " "; Prints: 9x 14 foo fooa foolio Foolio foo12 foo12a Foo12a foo12z foo13a (Or "foo12a" + "Foo12a" and "foolio" + "Foolio" and might be switched, depending on your locale.) DESCRIPTION
This module exports two functions, "nsort" and "ncmp"; they are used in implementing my idea of a "natural sorting" algorithm. Under natural sorting, numeric substrings are compared numerically, and other word-characters are compared lexically. This is the way I define natural sorting: o Non-numeric word-character substrings are sorted lexically, case-insensitively: "Foo" comes between "fish" and "fowl". o Numeric substrings are sorted numerically: "100" comes after "20", not before. o W substrings (neither words-characters nor digits) are ignored. o Our use of w, d, D, and W is locale-sensitive: Sort::Naturally uses a "use locale" statement. o When comparing two strings, where a numeric substring in one place is not up against a numeric substring in another, the non-numeric always comes first. This is fudged by reading pretending that the lack of a number substring has the value -1, like so: foo => "foo", -1 foobar => "foo", -1, "bar" foo13 => "foo", 13, foo13xyz => "foo", 13, "xyz" That's so that "foo" will come before "foo13", which will come before "foobar". o The start of a string is exceptional: leading non-W (non-word, non-digit) components are are ignored, and numbers come before letters. o I define "numeric substring" just as sequences matching m/d+/ -- scientific notation, commas, decimals, etc., are not seen. If your data has thousands separators in numbers ("20,000 Leagues Under The Sea" or "20.000 lieues sous les mers"), consider stripping them before feeding them to "nsort" or "ncmp". The nsort function This function takes a list of strings, and returns a copy of the list, sorted. This is what most people will want to use: @stuff = nsort(...list...); When nsort needs to compare non-numeric substrings, it uses Perl's "lc" function in scope of a <use locale>. And when nsort needs to lowercase things, it uses Perl's "lc" function in scope of a <use locale>. If you want nsort to use other functions instead, you can specify them in an arrayref as the first argument to nsort: @stuff = nsort( [ &string_comparator, # optional &lowercaser_function # optional ], ...list... ); If you want to specify a string comparator but no lowercaser, then the options list is "[&comparator, '']" or "[&comparator]". If you want to specify no string comparator but a lowercaser, then the options list is "['', &lowercaser]". Any comparator you specify is called as "$comparator->($left, $right)", and, like a normal Perl "cmp" replacement, must return -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument. Any lowercaser function you specify is called as "$lowercased = $lowercaser->($original)". The routine must not modify its $_[0]. The ncmp function Often, when sorting non-string values like this: @objects_sorted = sort { $a->tag cmp $b->tag } @objects; ...or even in a Schwartzian transform, like this: @strings = map $_->[0] sort { $a->[1] cmp $b->[1] } map { [$_, make_a_sort_key_from($_) ] @_ ; ...you wight want something that replaces not "sort", but "cmp". That's what Sort::Naturally's "ncmp" function is for. Call it with the syntax "ncmp($left,$right)" instead of "$left cmp $right", but otherwise it's a fine replacement: @objects_sorted = sort { ncmp($a->tag,$b->tag) } @objects; @strings = map $_->[0] sort { ncmp($a->[1], $b->[1]) } map { [$_, make_a_sort_key_from($_) ] @_ ; Just as with "nsort" can take different a string-comparator and/or lowercaser, you can do the same with "ncmp", by passing an arrayref as the first argument: ncmp( [ &string_comparator, # optional &lowercaser_function # optional ], $left, $right ) You might get string comparators from Sort::ArbBiLex. NOTES
o This module is not a substitute for Sort::Versions! If you just need proper version sorting, use that! o If you need something that works sort of like this module's functions, but not quite the same, consider scouting thru this module's source code, and adapting what you see. Besides the functions that actually compile in this module, after the POD, there's several alternate attempts of mine at natural sorting routines, which are not compiled as part of the module, but which you might find useful. They should all be working implementations of slightly different algorithms (all of them based on Martin Pool's "nsort") which I eventually discarded in favor of my algorithm. If you are having to naturally-sort very large data sets, and sorting is getting ridiculously slow, you might consider trying one of those discarded functions -- I have a feeling they might be faster on large data sets. Benchmark them on your data and see. (Unless you need the speed, don't bother. Hint: substitute "sort" for "nsort" in your code, and unless your program speeds up drastically, it's not the sorting that's slowing things down. But if it is "nsort" that's slowing things down, consider just: if(@set >= SOME_VERY_BIG_NUMBER) { no locale; # vroom vroom @sorted = sort(@set); # feh, good enough } elsif(@set >= SOME_BIG_NUMBER) { use locale; @sorted = sort(@set); # feh, good enough } else { # but keep it pretty for normal cases @sorted = nsort(@set); } o If you do adapt the routines in this module, email me; I'd just be interested in hearing about it. o Thanks to the EFNet #perl people for encouraging this module, especially magister and a-mused. COPYRIGHT AND DISCLAIMER
Copyright 2001, Sean M. Burke "sburke@cpan.org", all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. AUTHOR
Sean M. Burke "sburke@cpan.org" perl v5.10.0 2004-12-30 Sort::Naturally(3pm)
All times are GMT -4. The time now is 12:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy