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
# 8  
Old 03-07-2016
Quote:
Originally Posted by looney
.
.
.
how can i use OFS of awk in bash.?
.
.
.
You can't. And, by the way, using OFS wouldn't solve your problem either. How about
Code:
while IFS=";" read -r -a arr; do printf "%013.f" ${arr[0]}; unset arr[0]; printf ";%s" ${arr[*]}; printf "\n"; done < file

# 9  
Old 03-07-2016
Using awk printf %f works fine for this for numeric fields up to about 16 digits. If you have longer fields or if you have fields that contain alphanumeric strings, you could also try something like:
Code:
#!/bin/ksh
# Usage: script_name [ file [ min_field1_width ]]

# We don't want an old file hanging around if no lines are padded this time.
rm -f padded

# Pad first field to N characters with leading zeros.
awk -v N="${2:-13}" '
BEGIN {	FS = OFS = ";"
	Z = sprintf("%0*d", N, 0)
}
(L = length($1)) < N {
	$1 = substr(Z, 1, N - L) $1
	print > "padded"
}
1' "${1:-file}"

This was written and tested using a Korn shell but should work with any POSIX-conforming shell. If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

If you invoke this script with no arguments, it defaults to using file as the name of the file to be processed and 13 characters minimum for the first field producing the output you requested. If you invoke it with:
Code:
./script_name file 20

where file is the name of a file containing the input data you provided in post #1 in this thread, it will pad the 1st field of the contents of file with leading zeros to produce no less than twenty characters in that field:
Code:
00000000790069375798;94019;DIR-505/E;8;15.12;D-Link;CMT
00000004039784518926;94045;2.633-002.0;2;13.00;Kärcher;CMT
00000008806085027084;92798;CLT-W406/SEE;1;9.16;Samsung;CMT
00000005025232683680;94246;DMC-FZ200;10;251.26;Panasonic;CMT
00000000885370404449;94571;3LR-00001;10;30.25;Microsoft;CMT

and the file named padded will contain a copy of the above as well (since every line is padded with seven or eight zeros).
# 10  
Old 03-07-2016
Without an array it becomes easier
Code:
while IFS=";" read -r a1 a0
do
  printf "%013.f;%s\n" "$a1" "$a0"
done < file

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