Sponsored Content
Top Forums Shell Programming and Scripting CSV formatting with prefixing, appending and padding field Post 302274550 by Annihilannic on Thursday 8th of January 2009 12:49:19 AM
Old 01-08-2009
No need to run sed multiple times; you can have multiple commands in a sed script. Try this:

Code:
sed '
  s/^\(....\)\(.*\)/\2      \1/
  s/^.*/CODE,&/
  s/$/,,,,,,,,,,,,,,,,,,/
' input.csv > output.csv

The second command there identifies a 4 character substring and a rest-of-string substring, then reinserts them into the replacement with a six-character separator (much like & does, but it inserts the entire match).

However you could simplify that further and do it in one step, e.g.:

Code:
sed 's/^\(....\)\(.*\)/CODE,\2      \1,,,,,,,,,,,,,,,,,,/' input.csv > output.csv

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Keeping padding in a date field

Hi Guys, I'm having a bit of a problem with a script, i need to get the day, month and day of month into a string, so i'm using: CURRENT_DATE=`date +"%a %b %e"` It is getting the correct date out, however it is not keeping the padding on the day of month. The %e is supposed to pad the day... (5 Replies)
Discussion started by: seanbyrne
5 Replies

2. Shell Programming and Scripting

Need help appending a string to a field

Hi, This is driving me nuts, can't think of any easy way to do it. I need to append a string ".00" only in the third field of a file, and only if it does NOT have a decimal point already Here is what the file looks like- 1400030846,2,17,POL GENERAL 1400030900,3,14.95,FIC GENERAL If... (7 Replies)
Discussion started by: sfisk
7 Replies

3. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 Replies

4. Shell Programming and Scripting

Help with appending to a csv file

I am working to import a file and have almost all the information accounted for except this last item. First of all the file name is the category and this is what I need to append to the file. The catch I believe is the fact the file name has spaces in it. Filename CPU Products.csv File... (3 Replies)
Discussion started by: nkr1ptd
3 Replies

5. Shell Programming and Scripting

Appending a parameter value to a .csv file???

Hi I have a date which I get as parameter. I want it to be added as first column in all the rows in a csv file. I have tried the below code but no success. date=$1 awk -F"," '{print $date","$0}' z3.csv > z4.csv Could you tell the correct code for the above req.? How to use code... (1 Reply)
Discussion started by: msp2244
1 Replies

6. Shell Programming and Scripting

Appending = in particular column in csv file

Hi, I have a requirement to append = in particular row in csv file. Data in csv is as follow: row1,a,a,a row2,b,b,b row3,c,c,c row4,d,d,d csv should be modified at row3 and no. of columns are not fixed but rows are. output should be as: row1,a,a,a row2,b,b,b row3,=c,=c,=c... (2 Replies)
Discussion started by: Divya1987
2 Replies

7. 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

8. Shell Programming and Scripting

Padding a csv value with 0's

I have this csv file that I would like to sort on the 20th and 21st field. They are high lighted below. My challenge is that when I sort on those fields they are not in order as I would have liked. It seems like I have to pad those fields to the longest value in that fields data. ... (6 Replies)
Discussion started by: GroveTuckey
6 Replies

9. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

10. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies
Text::CSV::Encoded(3pm) 				User Contributed Perl Documentation				   Text::CSV::Encoded(3pm)

NAME
Text::CSV::Encoded - Encoding aware Text::CSV. SYNOPSIS
# Here in Perl 5.8 or later $csv = Text::CSV::Encoded->new ({ encoding_in => "iso-8859-1", # the encoding comes into Perl encoding_out => "cp1252", # the encoding comes out of Perl }); # parsing CSV is regarded as input $csv->parse( $line ); # $line is a iso-8859-1 encoded string @columns = $csv->fields(); # they are unicode data # combining list is regarded as output $csv->combine(@columns); # they are unicode data $line = $csv->string(); # $line is a cp1252 encoded string # if you want for returned @columns to be encoded in $encoding # or want for combining @columns to be assumed in $encoding $csv->encoding( $encoding ); # change input/output encodings $csv->encoding_in('shiftjis')->encoding_out('utf8'); $csv->eol(" "); open (my $in, "sjis.csv"); open (my $out, "output.csv"); # change an encoding from shiftjis to utf8 while( my $columns = $csv->getline( $in ) ) { $csv->print( $out, $columns ); } close($in); close($out); # simple shortcuts # (regardless of encoding_in/out and encoding) $uni_columns = $csv->decode( 'euc-jp', $line ); # euc-jp => unicode $line = $csv->encode( 'euc-jp', $uni_columns ); # unicode => euc-jp # pass check value to coder class $csv->coder->encode_check_value( Encode::FB_PERLQQ ); DESCRIPTION
This module inherits Text::CSV and is aware of input/output encodings. ENCODINGS
Acceptable names of encodings ("encoding_in", "encoding_out" and "encoding") are depend upon its coder class (see to "CODER CLASS"). But these names should be based on Encode supported names. See to Encode::Supported and Encode::Alias. METHODS
new $csv = Text::CSV::Encoded->new(); Text::CSV::Encoded->error_diag unless $csv; # report error message Creates a new Text::CSV::Encoded object. It can take all options of Text::CSV. Of course, "binary" option is always on. If Text::CSV::Encoded fails in constructing, you can get an error message using "error_diag". See to "error_diag" in Text::CSV. The following options are supported by this method: encoding The encoding of list data in below cases. * list data returned by fields() after successful parse(). * list data consumed by combine(). * list reference returned by getline(). * list reference taken by print(). See to "encoding". encoding_in encoding_io_in encoding_to_parse The encoding for pre-parsing CSV strings. See to "encoding_in". "encoding_io_in" is an alias to "encoding_in". If both "encoding_in" and "encoding_io_in" are set at the same time, the "encoding_in" takes precedence. "encoding_to_parse" is an alias to "encoding_in". If both "encoding_in" and "encoding_to_parse" are set at the same time, the "encoding_in" takes precedence. encoding_out encoding_io_out encoding_to_combine The encoding for combined CSV strings. See to "encoding_out". "encoding_io_out" is an alias to "encoding_out". If both "encoding_out" and "encoding_io_out" are set at the same time, the "encoding_out" takes precedence. "encoding_to_combine" is an alias to "encoding_out". If both "encoding_out" and "encoding_io_out" are set at the same time, the "encoding_out" takes precedence. coder_class A name of coder class that really decodes and encodes data. encoding_in $csv = $csv->encoding_in( $encoding ); The accessor to an encoding for pre-parsing CSV strings. If no encoding is given, returns current $encoding, otherwise the object itself. $encoding = $csv->encoding_in() In "parse" or "getline", the $csv will assume CSV data as the given encoding. If "encoding_in" is not specified or is set with false value (undef), it will assume input CSV strings as Unicode (not UTF-8) when Text::CSV::Encoded::Coder::Encode is used. $csv->encoding_in( undef ); # assume as Unicode when Text::CSV::Encoded::Coder::Encode is used. If you pass a list reference that contains multiple encodings to the method, the working are depend upon the coder class. For example, if you use the coder class with Text::CSV::Encoded::Coder::EncodeGuess, it might guess the encoding from the given list. $csv->coder_class( 'Text::CSV::Encoded::Coder::EncodeGuess' ); $csv->encoding_in( ['shiftjis', 'euc-jp', 'iso-20022-jp'] ); See to "Coder Class" and Text::CSV::Encoded::Coder::EncodeGuess. encoding_out $csv = $csv->encoding_out( $encoding ); The accessor to an encoding for converting combined CSV strings. If no encoding is given, returns current $encoding, otherwise the object itself. $encoding = $csv->encoding_out(); In "combine" or "print", the $csv will return a result string encoded in the given encoding. If "encoding_out" is not specified or is set with false value, it will return a result string as Unicode (not UTF-8). $csv->encoding_out( undef ); # return as Unicode when Text::CSV::Encoded::Coder::Encode is used. You must not pass a list reference to "encoding_out", unlike "encoding_in" or "encoding". encoding $csv = $csv->encoding( $encoding ); $encoding = $csv->encoding(); The accessor to an encoding for list data in the below cases. * list data returned by fields() after successful parse(). * list data consumed by combine(). * list reference returned by getline(). * list reference taken by print(). In other word, in "parse" and "getline", "encoding" is an encoding of the returned list. And in "combine" and "print", it is assumed as an encoding for the passing list data. If "encoding" is not specified or is set with false value ("undef"), the field data will be regarded as Unicode (when Text::CSV::Encoded::Coder::Encode is used). # ex.) a souce code is encoded in euc-jp, and print to stdout in shiftjis. @fields = ( .... ); $csv->encoding('euc-jp') ->encoding_to_combine('shiftjis') # same as encoding_out ->combine( @fields ); # from euc-jp to shift_jis print $csv->string; $csv->encoding('shiftjis') ->encoding_to_parse('shiftjis') # same as encoding_in ->parse( $csv->string ); # from shift_jis to shift_jis print join(", ", $csv->fields ); If you pass a list reference contains multiple encodings to the method, The working are depend upon the coder class. For example, Text::CSV::Encoded::EncodeGuess might guess the encoding from the given list. $csv->coder_class( 'Text::CSV::Encoded::Coder::EncodeGuess' ); $csv->encoding( ['ascii', 'ucs2'] )->combine( @cols ); See to "Coder Class" and Text::CSV::Encoded::Coder::EncodeGuess. parse/combine/getline/print $csv->parse( $encoded_string ); @unicode_array = $csv->fields(); $csv->combine( @unicode_array ); $encoded_string = $csv->string; $unicode_arrayref = $csv->getline( $io ); # get arrayref contains unicode strings $csv->print( $io, $unicode_arrayref ); # print $io with string encoded in $csv->encoded_in. $encoded_arrayref = $csv->getline( $io => $encoding ) # directly encoded in $encoding. Here is the relation of "encoding_in", "encoding_out" and "encoding". # CSV string => (getline/parsed) => Perl array # assumed as encoded in # encoding_in encoding # Perl array => (print/combined) => CSV string # assumed as encoded in # encoding encoding_out If you want to treat Perl array data as Unicode in Perl5.8 and later, don't specify "encoding" (or set "undef" into "encoding"). decode $arrayref = $csv->decode( $encoding, $encoded_string ); $arrayref = $csv->decode( $string ); A short cut method to convert CSV to Perl. Without $encoding, $string is assumed as a Unicode. The returned value status is depend upon its coder class. With Text::CSV::Encoded::Coder::Encode, $arrayref contains Unicode strings. encode $encoded_string = $csv->encode( $encoding, $arrayref ); $string = $csv->encode( $arrayref ); A short cut method to convert Perl to CSV. With Text::CSV::Encoded::Coder::Encode, $arrayref is assumed to contain Unicode strings. Without $encoding, return as is. coder_class $csv = $csv->coder_class( $classname ); $classname = $csv->coder_class(); Returns the coder class name. See to "CODER CLASS". coder $coder = $csv->coder(); Returns a coder object. CODER CLASS
Text::CSV::Encoded delegates the encoding converting process to another module. Since version 5.8, Perl standardly has Encode module. So the default coder module Text::CSV::Encoded::Coder::Encode also uses it. In this case, you don't have to take care of it. In older Perl, the default is Text::CSV::Encoded::Coder::Base. It does nothing. So you have to make a coder module using your favorite converting module, for example, Unicode::String or Jcode and so on. Please check Text::CSV::Encoded::Coder::Base and Text::CSV::Encoded::Coder::Encode to make such a module. In calling Text::CSV::Encoded, you can set another coder module with "coder_class"; use Text::CSV::Encoded coder_class => 'YourCoder'; This will call "YourCoder" module in runtime. Use Encode module Perl 5.8 or later, Text::CSV::Encoded use Text::CSV::Encoded::Coder::Encode as its backend engine. You can set "encoding_in", "encoding_out" and "encoding" with Encode supported encodings. See to Encode::Supported and Encode::Alias. Without "encoding" (or set "undef"), "parse"/"getline"/"getline_hr" return list data whose entries are "Unicode" strings. On the contrary, "combine"/"print" take data as "Unicode" string list. About the extra methods "decode" and "encode". "decode" returns "Unicode" string list and "encode" takes "Unicode" string list. But If no $encoding is passed to "encode", it returns a non-Unicode CSV string for non-Unicode list data. Use Encode::Guess module If you don't know definitely input CSV data encoding (for parse/getline), Text::CSV::Encoded::Coder::EncodeGuess may be useful to you. It inherits from Text::CSV::Encoded::Coder::Encode, so you can treate methods and attributes as same as Text::CSV::Encoded::Coder::Encode. And it provides a guessing fucntion with Encode::Guess. When it is backend coder class, "encoding_in" and "encoding" can take a encoding list reference, and then it might guess the encoding from the given list. $csv->encoding_in( ['shiftjis', 'euc-jp'] )->parse( $sjis_or_eucjp_encoded_csv_string ); It is important to remember the guessing feature is not always successful. Or, the method can be applied to "encoding". For exmaple, you want to convert data from Microsoft Excel to CSV. use Text::CSV::Encoded coder_class => 'Text::CSV::Encoded::Coder::EncodeGuess'; use Spreadsheet::ParseExcel; my $csv = Text::CSV::Encoded->new( eol => " " ); $csv->encoding( ['ucs2', 'ascii'] ); # guessing ucs2 or ascii? $csv->encoding_out('shiftjis'); # print in shift_jis my $excel = Spreadsheet::ParseExcel::Workbook->Parse( $file ); my $sheet = $excel->{Worksheet}->[0]; for my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) { my @fields; for my $col ( $sheet->{MinCol} .. $sheet->{MaxCol} ) { my $cell = $sheet->{Cells}[$row][$col]; push @fields, $cell->{Val}; } $csv->print( @fields ); } In this case, guessing for list data. After combining, you may have a need to clear "encoding". Again remember that the feature is not always successful. In addtion, Microsoft Excel data converting is a carefult thing. See to "CAVEATS" in Text::CSV_XS. Use XXX module Someone might make a new coder module in older version Perl... There is an example with Jcode in Text::CSV::Encoded::Coder::Base document. TODO
More sophisticated tests - Welcome! Speed SEE ALSO
Text::CSV, Text::CSV_XS, Encode, Encode::Guess, utf8, Text::CSV::Encoded::Coder::Base, Text::CSV::Encoded::Coder::Encode, Text::CSV::Encoded::Coder::EncodeGuess AUTHOR
Makamaka Hannyaharamitu, <makamaka[at]cpan.org> The basic idea for this module and suggestions were given by H.Merijn Brand. He and Juerd advised me many points about documents and sources. COPYRIGHT AND LICENSE
Copyright 2008-2010 by Makamaka Hannyaharamitu This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2010-04-26 Text::CSV::Encoded(3pm)
All times are GMT -4. The time now is 11:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy