Add string based on character length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add string based on character length
# 1  
Old 11-03-2016
Add string based on character length

Good day, I am a newbie here and thanks for accepting me

I have a task to modify input data where my input data looks like
Code:
123|34567|CHINE
1|23|INDIA
34512|21|USA
104|901|INDIA

See that my input has two columns with different character length but max length is 5 and minimum length is 0 which is column 1 & column 2, for example first row is 123|34567 with character length is 3|5

I need to produce an output that all character must be in 5|5 character length, so when its only 3 character then put 00 in front, when character length is 1 then put 0000 in front

based on my file input, desired output should be like
Code:
00123|34567|CHINE
00001|00023|INDIA
34512|00021|USA
00104|00901|INDIA

please help

Last edited by fastlearner; 11-03-2016 at 01:12 AM..
# 2  
Old 11-03-2016
Welcome fastlearner,
You can do it with awk :
Code:
awk -F\| '{$1=sprintf("%05d",$1);$2=sprintf("%05d",$2)}OFS=FS' file

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Function needs to be called based on its first character in a supplied string

Hi All, I want to write a bash script in which a function needs to be called based on its first character in a supplied string. eg function "j" should be called when "jab" or "jgh" or "j" .... etc is hit. I have used complete -F in below script, however here function is invoked... (1 Reply)
Discussion started by: temp.sha
1 Replies

2. UNIX for Advanced & Expert Users

Replacing string length based on pattern

Hi All, I have a file which is like below. I need to read all the patterns that starts with P and then replace the 9 digit values to 8 digit values (remove leading integer). Can you please help Example : ( Please look below File) File : P,1 M1,... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

3. 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

4. Shell Programming and Scripting

Delimit file based on character length using awk

Hi, I need help with one problem, I came across recently. I have one input file which I need to delimit based on character length. $ cat Input.txt 12345sda231453 asd760kjol62569 sdasw4g76gdf57 And, There is one comma separated file which mentions "start of the field" and "length... (6 Replies)
Discussion started by: Prathmesh
6 Replies

5. Shell Programming and Scripting

Sort a hash based on the string length of the values

Hi, I want to be able to sort/print a hash based on the string length of the values. For example %hash = ( key1 => 'jeri', key2 => 'corona', key3 => 'una, ); I want to be able to print in the following order (smallest to largest) una,jeri,corona OR... (1 Reply)
Discussion started by: jdilts
1 Replies

6. Shell Programming and Scripting

Remove 3rd character from the end of a random-length string

Hi, I hope someone can share there scripting fu on my problem, I would like to delete the 3rd character from a random length of string starting from the end Example Output Hope you can help me.. Thanks in advance.. (3 Replies)
Discussion started by: jao_madn
3 Replies

7. Shell Programming and Scripting

Add character based on record length

All, I can't seem to find exactly what I'm looking for, and haven't had any luck patching things together. I need to look through a file, and if the record length is not 874, then add 'E' in position 778. Your help is greatly appreciated. (4 Replies)
Discussion started by: CutNPaste
4 Replies

8. Shell Programming and Scripting

Parsing 286 length Character string

Hi Friends, I have .txt file which has 13000 records. Each record is 278 character long. I am using below code to extract the string and it takes almost 10 minutes. Any suggestion please. cat filename.txt|while read line do f1=`echo $line|awk '{print substr($1,1,9)}'` f2=`echo... (6 Replies)
Discussion started by: ppat7046
6 Replies

9. UNIX for Dummies Questions & Answers

Cutting a string based on the third occcurence of a character

Hello, I am new to unix hence struggling with my requirement. I have a string like : ECR/CHQ/GBP/12345.out I need to get only the ECR/CHQ/GBP portion of the string(cut the string based on the third occurrence of / )...How do it do it? Many thanks (3 Replies)
Discussion started by: valluvan
3 Replies

10. Solaris

Calculate the length and add some string infront

Hi all, I have file having 4 coulmn like 9246309198,406655682,400009246309198 9246309198,9246309198,400009246309198 9246309198,9246309198,400009246309198 9246309198,9246309198,400009246309198 9246309198,406655682,400009246309198 9246309198,9246309198,400009246309198 I want to valid... (4 Replies)
Discussion started by: salaathi
4 Replies
Login or Register to Ask a Question