Append 0's based on length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append 0's based on length
# 1  
Old 04-05-2017
Append 0's based on length

I'm having data like this,

Code:
"8955719","186497034","0001","M","3"
"8955719","186497034","0002","M","10"
"8955719","186497034","0003","M","10"
"8955719","186497034","0004","M","3"
"8955723","186499034","0001","M","3"
"8955723","186499034","0002","M","10"
"8955723","186499034","0003","M","10"
"8955723","186499034","0004","M","3"

My requirement is to change last column values to 4 digits based on value length. if value is single digit then we need to append three 0's in front of the value. if 2 two digit then two zeros need to append two 0's.

expected output :
Code:
"8955719","186497034","0001","M","0003"
"8955719","186497034","0002","M","0010"
"8955719","186497034","0003","M","0010"
"8955719","186497034","0004","M","0003"
"8955723","186499034","0001","M","0003"
"8955723","186499034","0002","M","0010"
"8955723","186499034","0003","M","0010"
"8955723","186499034","0004","M","0003"

Please help me.

Thanks in advance.

Last edited by rbatte1; 04-05-2017 at 11:13 AM.. Reason: Added CODE tags
# 2  
Old 04-05-2017
Quote:
Originally Posted by Artlk
...
My requirement is to change last column values to 4 digits based on value length. if value is single digit then we need to append three 0's in front of the value. if 2 two digit then two zeros need to append two 0's.

...
Question: what if the last column has a number with 5 or more digits? Would you want to retain the number or the length?

The code below retains the number in that case.
Code:
$ 
$ cat data_1.txt
"8955719","186497034","0001","M","3"
"8955719","186497034","0002","M","10"
"8955719","186497034","0003","M","10"
"8955719","186497034","0004","M","3"
"8955723","186499034","0001","M","3"
"8955723","186499034","0002","M","10"
"8955723","186499034","0003","M","10"
"8955723","186499034","0004","M","3"
"8955723","186499034","0004","M","1234"
"8955723","186499034","0004","M","12345"
"8955723","186499034","0004","M","123456"
"8955723","186499034","0004","M","1234567"
$ 
$ perl -F, -lane 'BEGIN{$,=","} $F[4] =~ s/(")(\d+)(")/$1.sprintf("%04d",$2).$3/eg; print @F' data_1.txt
"8955719","186497034","0001","M","0003"
"8955719","186497034","0002","M","0010"
"8955719","186497034","0003","M","0010"
"8955719","186497034","0004","M","0003"
"8955723","186499034","0001","M","0003"
"8955723","186499034","0002","M","0010"
"8955723","186499034","0003","M","0010"
"8955723","186499034","0004","M","0003"
"8955723","186499034","0004","M","1234"
"8955723","186499034","0004","M","12345"
"8955723","186499034","0004","M","123456"
"8955723","186499034","0004","M","1234567"
$
$

# 3  
Old 04-05-2017
Hi durden_tyler,

Thank you, its working. Smilie Smilie

as of now last column length will not exceed 4 char.
# 4  
Old 04-05-2017
Try:
Code:
awk '{split($NF,F,/"/); $NF=sprintf("\"%04d\"",F[2])}1' FS=, OFS=, file

--
or, using double quotes as the field separator:
Code:
awk '{$(NF-1)=sprintf("%04d", $(NF-1))}1' FS=\" OFS=\" 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

Select lines based on character length

Hi, I've got a file like this: 22 22:35645163:T:<CN0>:0 0 35645163 T <CN0> 22 rs140738445:20902439:TTTTTTTG:T 0 20902439 T TTTTTTTG 22 rs149602065:40537763:TTTTTTG:T 0 40537763 T TTTTTTG 22 rs71670155:50538408:TTTTTTG:T 0 50538408 T TTTTTTG... (3 Replies)
Discussion started by: zajtat
3 Replies

2. UNIX for Dummies Questions & Answers

To flat file, append null or space if its length is less than 10

Hi, We receive flat files with fixed width data Now our goal is append from right null or space to each record if the lenght of the record is less than for example 10. for example 123 45 6 0 123 45 123 45 6 123 and output should be 123 45 6 0 123 45**** 123 45 6**... (7 Replies)
Discussion started by: shharrath
7 Replies

3. Shell Programming and Scripting

Help required on Length based lookup

Hi, I have two files one (abc.txt) is having approx 28k records and another (bcd.txt) on is having 112k records, the length of each files are varried. I am trying to look up abc.txt file with bcd.txt based on length, where ever abc.txt records are matching with bcd.txt I am successful match... (8 Replies)
Discussion started by: rramkrishnas
8 Replies

4. Shell Programming and Scripting

Append spaces the rows to make it into a required fixed length file

I want to make a script to read row by row and find its length. If the length is less than my required length then i hav to append spaces to that paritucular row. Each row contains special characters, spaces, etc. For example my file contains , 12345 abcdef 234 abcde 89012 abcdefgh ... (10 Replies)
Discussion started by: Amrutha24
10 Replies

5. UNIX for Dummies Questions & Answers

Length of a segment based on coordinates

Hi, I would like to have the length of a segment based on coordinates of its parts. Example input file: chr11 genes_good3.gtf aggregate_gene 1 100 gene1 chr11 genes_good3.gtf exonic_part 1 60 chr11 genes_good3.gtf exonic_part 70 100 chr11 genes_good3.gtf aggregate_gene 200 1000 gene2... (2 Replies)
Discussion started by: fadista
2 Replies

6. Shell Programming and Scripting

Search for a particular field length and append '0' if less less than 10

Hi, I am new to Unix. Please help me in finding solution for the below scenario. I have a flat file that contains data like 378633410|3013505414|new_378633410|ALBERT|WALLS|378633410|Rew||||||| 351049045|239|new_351049045|JIM|COOK|351049045|Rew|||||||... (6 Replies)
Discussion started by: anandek
6 Replies

7. UNIX for Dummies Questions & Answers

Sorting words based on length

i need to write a bash script that recive a list of varuables kaka pele ronaldo beckham zidane messi rivaldo gerrard platini i need the program to print the longest word of the list. word in the output appears on a separate line and word order in the output is in the order Llachsicografi costs.... (1 Reply)
Discussion started by: yairpg
1 Replies

8. Shell Programming and Scripting

Split strings based on length

Hi All I am very much in need of help splitting strings based on length in Perl. e.g., Input text is : International NOUN Corp. NOUN 's POS Tulsa NOUN Output I want is : International I In Int Inte l al nal onal NOUN Corp. C Co Cor Corp . p. rp. orp. NOUN... (2 Replies)
Discussion started by: my_Perl
2 Replies

9. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

10. UNIX for Dummies Questions & Answers

Need find a file based length

Can some please help me? Want to find files over 35 characters in length? I am running HPUX. Would it be possible with find? Thanks in advance (8 Replies)
Discussion started by: J_ang
8 Replies
Login or Register to Ask a Question