Home > database >  awk to zero pad to string field of a csv file
awk to zero pad to string field of a csv file

Time:10-28

I need 6th column of a csv file to be padded with zeroes but when I try with below command, it just replaces the actual value to 0000.

awk -F',' -v OFS=',' '{$6 = sprintf("d", $6); print}' $Input

Input:

"xx","x","xxxxxx","xxx","xx","123","xxxxxxxxx","xxxxx"
"xx","x","xxxxxx","xxx","xx","23","xxxxxxxxx","xxxxx"
"xx","x","xxxxxx","xxx","xx","3","xxxxxxxxx","xxxxx"
"xx","x","xxxxxx","xxx","xx","4123","xxxxxxxxx","xxxxx"

Output:

"xx","x","xxxxxx","xxx","xx","0123","xxxxxxxxx","xxxxx"
"xx","x","xxxxxx","xxx","xx","0023","xxxxxxxxx","xxxxx"
"xx","x","xxxxxx","xxx","xx","0003","xxxxxxxxx","xxxxx"
"xx","x","xxxxxx","xxx","xx","4123","xxxxxxxxx","xxxxx"

CodePudding user response:

You may use this awk with a custom field separator ",":

awk 'BEGIN {FS=OFS="\",\""} {$6 = sprintf("d", $6)} 1' file

"xx","x","xxxxxx","xxx","xx","0123","xxxxxxxxx","xxxxx"
"xx","x","xxxxxx","xxx","xx","0023","xxxxxxxxx","xxxxx"
"xx","x","xxxxxx","xxx","xx","0003","xxxxxxxxx","xxxxx"
"xx","x","xxxxxx","xxx","xx","4123","xxxxxxxxx","xxxxx"

CodePudding user response:

Some non-awk ways to do this:

bash v5.1 includes a CSV command that can be enabled:

BASH_LOADABLES_PATH=${BASH/bin/lib}
enable -f csv csv

while IFS= read -r line; do
  csv -a row "$line"
  printf -v "row[5]" 'd' "${row[5]}"
  printf '"%s"\n' "${row[@]}" | paste -sd,
done < file

Ruby

ruby -rcsv -e '
  CSV.foreach(ARGV.shift) {|row|
    row[5] = row[5].rjust(4, "0")
    puts CSV.generate_line(row, force_quotes: true)
  }
' file

Perl with Text::CSV

perl -MText::CSV=csv -e '
  $input = csv(in => shift @ARGV);
  @padded = map {$_->[5] = sprintf "d", $_->[5]; $_} @$input;
  csv(in => \@padded, always_quote => 1)
' file
  • Related