Pad zeroes first field in a Delimited file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pad zeroes first field in a Delimited file
# 1  
Old 01-10-2012
Pad zeroes first field in a Delimited file

Need help. I tried using an awk command to pad zeroes. Unfortunately, the "|" pipe delimited character is gone when I tried to write the records to another file.

Code:
awk -F \| ' {$1=sprintf("%06s", $1); print $0}' $CUSTFINAL2 > $CUSTFINAL3

BEFORE
"KEYRECORD"|"SA ID"|"PER ID"|"SP ID"|"ACCT ID"|"PREM ID"|"PHONE"|"NAME"|"Name"|"MAIL ADDR1"|"MAIL ADDR2"|"MAIL CITY"|"MAIL STATE"|"MAIL 
1|"          "|"          "|"3197691605"|"          "|"3197691657"|"                              "|"OCCUPANT                                                        
2|"          "|"          "|"3246309010"|"          "|"3246309029"|"                              "|"OCCUPANT                               
3|"          "|"          "|"3350909705"|"          "|"3311776290"|"                              "|"OCCUPANT                                                        
3|"          "|"          "|"3311776205"|"          "|"3311776290"|"                              "|"OCCUPANT                                                        

AFTER
"KEYRECORD" "SA ID" "PER ID" "SP ID" "ACCT ID" "PREM ID" "PHONE" "NAME" "Name" "MAIL ADDR1" "MAIL ADDR2" "MAIL CITY" "MAIL STATE" "MAIL 
000001 "          " "          " "3197691605" "          " "3197691657" "                              " "OCCUPANT                                                        
000002 "          " "          " "3246309010" "          " "3246309029" "                              " "OCCUPANT                                                        
000003 "          " "          " "3350909705" "          " "3311776290" "                              " "OCCUPANT                                                        
000003 "          " "          " "3311776205" "          " "3311776290" "                              " "OCCUPANT                                                        
000004 "          " "          " "3329099805" "          " "3329099858" "                              " "OCCUPANT

# 2  
Old 01-10-2012
Set OFS:
Code:
awk -F \| ' {$1=sprintf("%06d", $1); print $0}' OFS=\| $CUSTFINAL2 > $CUSTFINAL3

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-10-2012
So much thanks Chubler_XL !!!!

---------- Post updated at 07:56 PM ---------- Previous update was at 07:29 PM ----------

Can I use printf in this awk to pad my variable 'n' counter???

Code:
	awk -F \| '
	    NR == 1 { print "\"KEYRECORD\"|" $0; next; }
	    {
       	 if( $4 == "\"          \"" && last5 != $5) {
       	     n++;
	        } 

	        print n "|" $0;
	        last = $4;
	        last5 = $5;

	    }
	'  $CUSTFINAL > $CUSTFINAL2

# 4  
Old 01-11-2012
Yes just use:
Code:
printf "%06d|%s\n", n, $0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace field in the delimited file

Hi, I have the requirement similar to the one mentioned in the below thread. https://www.unix.com/unix-for-dummies-questions-and-answers/128155-search-replace-string-only-particular-column-delimited-file.html The only difference is that I need to change the field for row 1,2 and the last... (14 Replies)
Discussion started by: chetanojha
14 Replies

2. Shell Programming and Scripting

How to pad zeroes based on input string?

Hello everyone, I am comparing two floating point numbers by storing them in seperate files and then using difference command to verify,it is working fine. But I want to compare two values which will come at 4 precision places. ex: file1 Date,Count,Checksum... (7 Replies)
Discussion started by: karthik adiga
7 Replies

3. Shell Programming and Scripting

Remove Last field from a delimited file

Hi, I have a '~' delimited file and i want to remove the last field using awk. Please find the sample records below: 1428128~1~0~1100426~003~50220~005~14~0~194801~11~0~3~14~0~50419052335~0~0820652001~2~00653862 ~0~1~0~00126~1~20000110~20110423~R~ ~0~Z~1662.94~ ~002041~0045~Z~... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

4. Shell Programming and Scripting

sed command to remove the first field from a '|' delimited file

Hi I have a file with fields delimited by |. I need to remove the first field from the file. I tried cut but it just extracts that field. sample.output abc|100|name1 cde|200|name2 efg|300|name3 Output should be sample.output 100|name1 200|name2 300|name3 thanks Var (6 Replies)
Discussion started by: var285
6 Replies

5. UNIX for Dummies Questions & Answers

Trim and pad a field in the middle of unix file

Hello, I have a file with several lines and I need to trim and pad with spaces the data that are between position 6 and 15 included. Data in position 1 to 5 and after 15 could be anything, and should stay as they are. For instance, the following records in a file (underscore = space)... (4 Replies)
Discussion started by: BSF
4 Replies

6. Shell Programming and Scripting

Remove leading zeroes in 2nd field using sed

Hi Forum. I tried searching the forum but couldn't find a solution for my question. I have the following data and would like to have a sed syntax to remove the leading zeroes from the 2nd field only: Before: 2010-01-01|123|1|1000|2000|500|1500|600|700... (18 Replies)
Discussion started by: pchang
18 Replies

7. UNIX for Dummies Questions & Answers

Insert Field into a tab-delimited file

Hello, I have about 100 files in a directory with fields which are tab delimited. I would like to append the file name as the first field and it has to be done as many times as the total lines in the file. For example, myFile1.txt has the following data: 1 x y z 2 a b ... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

8. Shell Programming and Scripting

insert a field into a tab delimited file

Hello, Can someone help me to do this with awk or sed? I have a file with multiple lines, each line has many fields separated with a tab. I would like to add one more field holding 'na' in between the first and second fields. old file looks like, 1, field1 field2 field3 ... 2, field1... (7 Replies)
Discussion started by: ssshen
7 Replies

9. Shell Programming and Scripting

Count field frequency in a '|' delimited file

I have a large file with fields delimited by '|', and I want to run some analysis on it. What I want to do is count how many times each field is populated, or list the frequency of population for each field. I am in a Sun OS environment. Thanks, - CB (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

10. Shell Programming and Scripting

How to trim the leading zeroes in a Currency field ?

How do I trim the leading zeroes, and (+,-) in the currency field ? I have a text file. Your bill of +00002780.96 for a/c no. 25287324 is due on 11-06. Your bill of +00422270.48 for a/c no. 28931373 is due on 11-06. I want the O/P file to be like. Your bill of 2780.96 for a/c no. 25287324... (22 Replies)
Discussion started by: Amruta Pitkar
22 Replies
Login or Register to Ask a Question