awk pad 1 column with leading zero if char > 12


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk pad 1 column with leading zero if char > 12
# 1  
Old 03-07-2016
awk pad 1 column with leading zero if char > 12

Hello,

I got a question. I have several csv files with lots of data in it and for the first column i have EAN codes.

The problem that i am facing is that some of these codes have the leading 0 removed so they are 12 or less chars while a EAN code is (always?) 13 chars.

For this i used a sed script:
Code:
sed 's/\<[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\>/0&/'

So far this works fine but i have no check on this to see how many lines have the extra 0 added. Is there a way to do this in awk and have a seperate file with the padded codes? Of course i need to save the rest of the data that is present in the files also.

Some sample code is below:
Code:
790069375798;94019;DIR-505/E;8;15.12;D-Link;CMT
4039784518926;94045;2.633-002.0;2;13.00;Kärcher;CMT
8806085027084;92798;CLT-W406/SEE;1;9.16;Samsung;CMT
5025232683680;94246;DMC-FZ200;10;251.26;Panasonic;CMT
885370404449;94571;3LR-00001;10;30.25;Microsoft;CMT

In above sample code are the first and last line missing the leading 0.
# 2  
Old 03-07-2016
Would this do:
Code:
awk -F";" '{$1 = sprintf ("%013.f", $1)}  /^0/ {print > "padded"} 1' OFS=";" file
0790069375798;94019;DIR-505/E;8;15.12;D-Link;CMT
4039784518926;94045;2.633-002.0;2;13.00;Kärcher;CMT
8806085027084;92798;CLT-W406/SEE;1;9.16;Samsung;CMT
5025232683680;94246;DMC-FZ200;10;251.26;Panasonic;CMT
0885370404449;94571;3LR-00001;10;30.25;Microsoft;CMT
cat padded
0790069375798;94019;DIR-505/E;8;15.12;D-Link;CMT
0885370404449;94571;3LR-00001;10;30.25;Microsoft;CMT

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-07-2016
Quote:
Originally Posted by RudiC
Would this do:
Code:
awk -F";" '{$1 = sprintf ("%013.f", $1)}  /^0/ {print > "padded"} 1' OFS=";" file
0790069375798;94019;DIR-505/E;8;15.12;D-Link;CMT
4039784518926;94045;2.633-002.0;2;13.00;Kärcher;CMT
8806085027084;92798;CLT-W406/SEE;1;9.16;Samsung;CMT
5025232683680;94246;DMC-FZ200;10;251.26;Panasonic;CMT
0885370404449;94571;3LR-00001;10;30.25;Microsoft;CMT
cat padded
0790069375798;94019;DIR-505/E;8;15.12;D-Link;CMT
0885370404449;94571;3LR-00001;10;30.25;Microsoft;CMT

Hello,

Thank you for the quick response. It seems to work fine. All lines that arent 13 numbers get filled to 13 this way. The strange thing is that i had just about the same code before but it did not seem to work. The only difference that i can spot is that i used 13d instead of 13.f like your code.
# 4  
Old 03-07-2016
When I used %013d, it printed 0002147483647 which is the system's largest integer as shown by
Code:
getconf INT_MAX
2147483647

# 5  
Old 03-07-2016
Quote:
Originally Posted by RudiC
When I used %013d, it printed 0002147483647 which is the system's largest integer as shown by
Code:
getconf INT_MAX
2147483647

No wonder it went wrong there. I overlooked the correct syntax.
# 6  
Old 03-07-2016
bash
Code:
while IFS=";" read -r -a arr
do
if [[ ${#arr[0]}} < 13 ]]
then
arr[0]=`printf "%013.f" ${arr[0]}`
printf "%s;" ${arr[*]}
printf "\n"
fi
done < file

but i am getting ; at last of each records. Kindly help me on this.
how can i use OFS of awk in bash.?

0790069375798;94019;DIR-505/E;8;15.12;D-Link;CMT;
0885370404449;94571;3LR-00001;10;30.25;Microsoft;CMT;
# 7  
Old 03-07-2016
maybe a loop:
Code:
for i in $(seq 1 ${#arr[@]})
do
   [[ $i = ${#arr[@]} ]] && l="\n" || l=";"
   printf "%s$l" ${arr[$i - 1]}
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Inspecting leading char in string for slash

In a SCO Unix shop, I am working on the following script to move any file to its same location on the target machine (called 'othersy' here): pwd=`pwd` for i in "$@" do echo " $i " if ; then echo 1; else echo 0; fi rcp -p $i othersy:$pwd/$i echo "Finished with ^ If I find a file... (4 Replies)
Discussion started by: wbport
4 Replies

2. Shell Programming and Scripting

Left pad spaces using awk or sed

Hi,I've a unix pipe delimited file as below f1|f2|f3|f4|f5|f6 My requirement is to pad spaces on the left to fields f2, f3 and f5. Field Lengths according to file layout f2 - 4 char f3 - 5 char f5 - 3 char If my record is as below 1|43|bc|h0|34|a Output record should be as below 1| 43| bc|h0|... (4 Replies)
Discussion started by: Soujanya_K
4 Replies

3. Shell Programming and Scripting

How to pad with leading zeros for current time?

I'm using cygwin bash to submit scheduled tasks (kinda like cron jobs) in windows and the following script is giving me grief. I need to format the current time with leading zeros before 10AM for the hour field. In this example, I manually typed in "09:50" instead of using the `printf...`... (2 Replies)
Discussion started by: siegfried
2 Replies

4. Shell Programming and Scripting

awk and leading zeroes

I have the following script that renames filenames like: blah_bleh_91_2011-09-26_00.05.43AM.xls and transforms it in: 91_20110926_000543_3_blih.xls for a in *.xls; do b="$(echo "${a}" | cut -d '_' -f4)" dia=`echo ${b} | cut -c9-10` mes=`echo ${b} | cut -c6-7` anio=`echo ${b} | cut -c1-4`... (4 Replies)
Discussion started by: Tr0cken
4 Replies

5. Shell Programming and Scripting

Removing leading zeros for a decimal column

removing leading zeros for a decimal column in a file which has string & decimal values ,,,,,6630140,XXXXXXXXXXXXXXX, 0020.00,USA ,,,,,6630150,XXXXXXXXXXXXXXXL (xyz, 0010.00,USA ,,,,,6630150,XXXXXXXXXXXXXXX(xyz), 1300.00,USA My file contains 9 columns. Out 9 columns, 8th column contains the... (9 Replies)
Discussion started by: marpadga18
9 Replies

6. Shell Programming and Scripting

Append char to the end of string from Nth column

I'm sure this is easy to do but I can't find a one line command with awk or sed to append a char to the end of the string from Nth column. Any sugestion please? Thanks (2 Replies)
Discussion started by: cabrao
2 Replies

7. Shell Programming and Scripting

Fixed Width Join & Pad Sed/Awk Help

I was wondering someone might be able to push me in the right direction, I am writing a script to modify fixed-width spool files, As you can see below the original spool file broke a single line into two for printability sake. I have had been able do the joins using sed, the thing I am... (10 Replies)
Discussion started by: Cho Nagurai
10 Replies

8. Shell Programming and Scripting

truncating leading zeros of a column in a file

Hi I have a file in which I have 5 columns which are delimited by “|” as shown ABC|12|YAK|METRIC|000000019.5 XYZ|10|ABX|META|000000002.5 Now my requirement is to take the last column trim the leading zero's for that column values and write back to the same file in the same... (7 Replies)
Discussion started by: nvuradi
7 Replies

9. UNIX for Dummies Questions & Answers

trim leading zero in certain column in a string

I would like to know how to trim leading zero only in certain column of of a string, example: hdhshdhdhd000012mmmm0002abc <===== before hdhshdhdhd 12mmmm 2abc <===== after Thanks for your help. (2 Replies)
Discussion started by: dngo
2 Replies

10. Shell Programming and Scripting

How to check a column contain numeric or char data type ??

I have a file called clientname_filename.csv whose contents are like col1|col2|col3|col4| 510|abc|xxx|450| 510|abc11|yyy|350 510|pqr99|zzz| 670 512|222|439|110 Here i have check the contents of column for data type. i have a constraints that col1 always contain Numeric value column 2... (12 Replies)
Discussion started by: jambesh
12 Replies
Login or Register to Ask a Question