Prepend 0 to a field in a record

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Prepend 0 to a field in a record
# 1  
Old 04-06-2018
Prepend 0 to a field in a record

Hi All,

I have a file like below. In the field 9 I am having 14,014,3,001/009 on the records. I want to convert the field to a three digit value. For example 14 should 014 , 3 should 003

Code:
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;14;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;014;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;1;3;29=0000;1.25
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;001/009;3;29=0000;1.25

Expected

Code:
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;014;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;014;003;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;1;003;29=0000;1.25
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;001/009;3;29=0000;1.25

# 2  
Old 04-06-2018
Any attempts / ideas / thoughts from your side?

Code:
awk '-F;' '{print $9}' file
11095
11095
11095
11095

# 3  
Old 04-06-2018
With the previous thread I tried to do like and getting all zeros

Code:
 awk '{ printf "%03d",$9}'  myfile

---------- Post updated at 10:51 PM ---------- Previous update was at 10:48 PM ----------

Quote:
Originally Posted by RudiC
Any attempts / ideas / thoughts from your side?

Code:
awk '-F;' '{print $9}' file
11095
11095
11095
11095


Sorry it is 12 the column

Code:
awk '-F;' '{print $12}' file

14
014
3
001/009

# 4  
Old 04-06-2018
Try:
Code:
awk '{$12=sprintf("%03d",$12)}1' FS=\; OFS=\; file

or
Code:
awk -F\; -v OFS=\; '{$12=sprintf("%03d",$12)}1' file

or
Code:
awk 'BEGIN{FS=OFS=";"} {$12=sprintf("%03d",$12)}1' file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-06-2018
In order to retain the special structure of the fourth line's $12, try
Code:
awk -F\; '$12 == $12+0 {$12 = sprintf ("%03d", $12)} 1' OFS=";" file
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;014;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;014;3;29=0000;1.25
11050;11001;;DEBITTRANC;529328;NRATL;;;11095;;-1;001;3;29=0000;1.25
11050;11001;;CREDITTRANC;5293218;NRATL;;;11095;;-1;001/009;3;29=0000;1.25

These 2 Users Gave Thanks to RudiC For This Post:
# 6  
Old 04-06-2018
Quote:
Originally Posted by arunkumar_mca
. . .
Sorry it is 12 the column

Code:
awk '-F;' '{print $12}' file

14
014
3
001/009

I'm pretty sure in your post#1's sample, the third line's $12 is 1 .
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace value in each field until a certain character in each record?

Each record coming with column names. I have to replace them in each record as shown below TIME=20181219110000261|CHAN=FMBKHJBAAAADPCFNAAAAAABA|EVNT=SWIclst|VALU=Session FMBKHJBAAAADPCFNAAAAAABA started|SRC=NSS|UCPU=0|SCPU=0 Output should look like: ... (9 Replies)
Discussion started by: sudhakar1987
9 Replies

2. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

3. Shell Programming and Scripting

Get last field specific record

i have file A as below contents --------------------------- Use descriptive thread titles when posting. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". For example, do not post questions For example, do not deliminated. output file as below:... (2 Replies)
Discussion started by: ANSHUMAN1983
2 Replies

4. Shell Programming and Scripting

Remove \n at the beginning of a field in a record.

Hi, In my file, I have few records which are split across multiple lines. File 1: ===== james,\n pre-auth completed,in patient,\n Fac_Id:23451,ramson,Dallas Expected is: ========== james,pre-auth completed,in patient,Fac_Id:23451,ramson,Dallas (8 Replies)
Discussion started by: machomaddy
8 Replies

5. Shell Programming and Scripting

awk text record - prepend first field to all subsequent fields

Hello everyone, I've suddenly gotten very interested in sed and awk (and enjoying it quite a bit too) because of a large conversion project that we're working on. I'm currently stuck with a very inefficient process for processing text blocks. I'm sure someone here should be able to easily point out... (2 Replies)
Discussion started by: jameswatson3
2 Replies

6. Shell Programming and Scripting

Replace third field of the first record in a file....

Hi, I am new to unix and am trying to do something below: I have a pipe delimited file with millions of records. I need to replace the third column of the first record to the number of lines in the file. How can I do that. Will appreciate any advice and help. Thanks Simi (3 Replies)
Discussion started by: simi28
3 Replies

7. Shell Programming and Scripting

get a field from a record

I have a file as: A,B,C,D,E G,H,I,J,K I need to find if fourth field is blank or has a space and print that line to other file. I tried using awk but am not getting the desired result. Pls help. (6 Replies)
Discussion started by: praveenK_Dudala
6 Replies

8. Shell Programming and Scripting

how to replace field for each record

Hello, I have the following xml formatted file. I would like to get the newnumber field number and replace into customernumber for each record. For example: <XMLFORMAT> <customernumberR11>9</customernumberR11> ... (12 Replies)
Discussion started by: happyv
12 Replies

9. Shell Programming and Scripting

modify a few field of the record information

Hello, I have the following record in a text file, i would like modify some field: 1 - remove all space between ",", but the company name of word will not delete. Anyway, I can use the following statement to do it. 's/^ *//;s/ *, */,/g;s/ *$//' file 2. field #12, I need to modify to time... (11 Replies)
Discussion started by: happyv
11 Replies
Login or Register to Ask a Question