Find max length of the field and then replace zero


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find max length of the field and then replace zero
# 1  
Old 03-07-2014
Hammer & Screwdriver Find max length of the field and then replace zero

hai guys,

pick the 1st field and calculate max length.
if suppose max length is 2,
then compare the all records if <2 then add zero's prefix of the record.
for ex:
Code:
s.no,sname
1,djud
37,jtuhe

in this max length of the 1st field is 2 right

the output wil be
Code:
s.no,sname
01,djud
37,jtuhe
 like this..


plz help me..
# 2  
Old 03-07-2014
Code:
sed "s/^\(.\),/0\1,/" file

# 3  
Old 03-07-2014
Try also
Code:
awk -F, 'NR==1
         NR>1   {T[++CNT]=$0
                 if (MAX<length($1)) MAX=length($1)}
         END    {for (i=1;i<=MAX;i++)   {split(T[i],L)
                                         printf "%0*d,%s\n", MAX, L[1], L[2]}}
        ' file

# 4  
Old 03-07-2014
Quote:
Originally Posted by RudiC
Try also
Code:
awk -F, 'NR==1
         NR>1   {T[++CNT]=$0
                 if (MAX<length($1)) MAX=length($1)}
         END    {for (i=1;i<=MAX;i++)   {split(T[i],L)
                                         printf "%0*d,%s\n", MAX, L[1], L[2]}}
        ' file

This code doesn't make much sense to me.

Shouldn't the for-loop condition be i<=CNT and not i<=MAX?

If hardcoding two fields into the printf statement, there is no need for the overhead of splitting each record twice.

The following has not been tested at all:
Code:
awk -F, 'NR==1
         NR>1   {T[NR,1]=$1; T[NR,2]=$2
                 if (MAX<length($1)) MAX=length($1)}
         END    {for (i=2; i<=NR; i++)
                     printf "%0*d,%s\n", MAX, T[i,1], T[i,2]}
        ' file

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 03-07-2014
Absolutely, I should have looked twice: it's CNT, not MAX.
# 6  
Old 03-08-2014
hai guys,

Thanks for the giving reply,

Can explain above script, because i am new to scripting.

Thanks.
# 7  
Old 03-08-2014
Code:
awk -F, 'NR==1                                          # print first line as is
         NR>1   {T[++CNT]=$0                            # save line in T array
                 if (MAX<length($1)) MAX=length($1)}    # calculate MAX length of 1. field
         END    {for (i=1;i<=CNT;i++)   {split(T[i],L)  # for all lines > 1, split line into fields
                  printf "%0*d,%s\n", MAX, L[1], L[2]}} # print formatted fields (f1 with "0" prefix)
        ' file

You should be able to understand Alister's improved version by yourself.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

2. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

3. Shell Programming and Scripting

Find field count and replace

Hello All, I have a file with contents like apple|ball|charlie|David| England|France|Germany| Ireland|Japan|King|London| Man|Nancy|Orange| here the column delimiter is | so if any of the lines/rows in the file has 3 only records (last field is empty), i want to place a | at the end of... (4 Replies)
Discussion started by: vinredmac
4 Replies

4. Shell Programming and Scripting

Aligning text files by max field length

Hello, Is there anyway that I can align a pipe delimited text file by the maxium field length where the field is separated out by pipes for large text files with more than 100,000 rows? So, far I have searched other forums and google about aligning text files in unix and I have noticed that... (7 Replies)
Discussion started by: physalis2099
7 Replies

5. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

6. Shell Programming and Scripting

Regular expression to find the length of a field

Hi, in the cobol copy books is there any regular expressions to be used in awk to fetch the length of each columns? below mentioned are the examples. Copy Book Sample 01 tablename. 02 group header. 03 col1 s9(10)V99. 03 Col2 s9(10)V9(3). 03 Col3 XXXX 02... (7 Replies)
Discussion started by: ahmedwaseem2000
7 Replies

7. Shell Programming and Scripting

How To Find Length of a Field in XML File

Hi I have a xml file with below data...have to find the length of the filedvalues... <?xml version="1.0" encoding="ISO-8859-15" standalone="no"?><abc xmlns:xsi="http://www.w3.org/2000/XMLSchem... (3 Replies)
Discussion started by: naughty21
3 Replies

8. UNIX for Dummies Questions & Answers

Find and replace a field in the last line

I have a file 'test.out' with contents: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| 4|1|10|10|I|hgfj| 34|0|10|10|I|sdg| I want to modify the fifth column with value 'I' to 'A' for only the last line. Below is what I expect to see: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| ... (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

9. Shell Programming and Scripting

find pattern and replace another field

HI all I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example: 100099C01101C00000000059394200701CREoperadora_TX 100099C01201C00000000000099786137OPERADORA_TX2 in the example above I need to change the first field from 1 to 2 only if... (3 Replies)
Discussion started by: sergiioo
3 Replies

10. UNIX for Dummies Questions & Answers

Length of a Unix filepath max length

Hi Guys, Could anyone shed some light on the length of a Unix filepath max length pls ? thanks ! Wilson (3 Replies)
Discussion started by: wilsontan
3 Replies
Login or Register to Ask a Question