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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a particular field length and append '0' if less less than 10
# 1  
Old 06-12-2012
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

Code:
378633410|3013505414|new_378633410|ALBERT|WALLS|378633410|Rew|||||||
351049045|239|new_351049045|JIM|COOK|351049045|Rew|||||||
351060382|770856|new_351060382|JANET|WHITLOCK|351060382|Rew|||||||
361443775|5058977|new_361443775|BRENDA|AHRENS|361443775|Rew|||||||

In this, I have to check the length of characters in second field(One in bold) and append '0's to make the field length 10.

So, after executing the command the output should look like

Code:
378633410|3013505414|new_378633410|ALBERT|WALLS|378633410|Rew|||||||
351049045|2390000000|new_351049045|JIM|COOK|351049045|Rew|||||||
351060382|7708560000|new_351060382|JANET|WHITLOCK|351060382|Rew|||||||
361443775|5058977000|new_361443775|BRENDA|AHRENS|361443775|Rew|||||||

The second field should always have 10 characters.

Please help me with your valuable inputs.

Thanks,
Anand.


Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 06-12-2012 at 11:17 AM.. Reason: code tags, see PM
# 2  
Old 06-12-2012
one way:

Code:
$ awk -F'|' 'BEGIN{OFS="|";zero="0000000000"}{$2=$2 substr(zero,1+length($2))}1' input

378633410|3013505414|new_378633410|ALBERT|WALLS|378633410|Rew|||||||
351049045|2390000000|new_351049045|JIM|COOK|351049045|Rew|||||||
351060382|7708560000|new_351060382|JANET|WHITLOCK|351060382|Rew|||||||
361443775|5058977000|new_361443775|BRENDA|AHRENS|361443775|Rew|||||||

would make more sense to prepend the zeros, maybe

Code:
$ awk 'BEGIN{FS=OFS="|"}{$2=sprintf("%010d",$2)}1' input
378633410|3013505414|new_378633410|ALBERT|WALLS|378633410|Rew|||||||
351049045|0000000239|new_351049045|JIM|COOK|351049045|Rew|||||||
351060382|0000770856|new_351060382|JANET|WHITLOCK|351060382|Rew|||||||
361443775|0005058977|new_361443775|BRENDA|AHRENS|361443775|Rew|||||||

# 3  
Old 06-12-2012
Another:
Code:
awk -F"|" -vOFS="|" '{$2=sprintf ("%-10d",$2);gsub(" ","0",$2)}1' file

# 4  
Old 06-12-2012
Without awk (just for the sake of curiosity), and in pure bash, maybe something like:

Code:
( while read line; do
     backfield=${line#*|}
     backfield=${backfield%%|*}
     field=$backfield
     lack=$(( 10 - ${#backfield} ))
     for ((i=0; i<$lack; i++)); do
          field=${field}0
          done
     line=${line/|$backfield|/|$field|}
     echo $line >>outputfile
     done ) < inputfile

--
Bye
# 5  
Old 06-12-2012
Quote:
Originally Posted by Lem
Without awk (just for the sake of curiosity), and in pure bash, maybe something like...
Keeping with that spirit, here's one in sh:
Code:
while IFS=\| read -r x y z; do
    while [ ${#y} -lt 10 ]; do
        y=${y}0
    done
    printf '%s|%s|%s\n' "$x" "$y" "$z"
done

Welcome to the forum, Lem.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 6  
Old 06-13-2012
Hi Everyone,

Many thanks for all your suggestions.
Infact each one of the solution worked absolutely worked fine.
Thanks once again for your timely help.

Regards,
Anand.
# 7  
Old 06-13-2012
Quote:
Originally Posted by alister
Keeping with that spirit, here's one in sh:
[...]
That's truly nice. Thank You. Smilie

Quote:
Welcome to the forum, Lem.
Thanks again, alister. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. What is on Your Mind?

Updated Forum Search Index Min Word Length to 2 Chars and Added Quick Search Bar

Today I changed the forum mysql database to permit 2 letter searches: ft_min_word_len=2 I rebuilt the mysql search indexes as well. Then, I added a "quick search bar" at the top of each page. I have tested this and two letter searches are working; but it's not perfect,... (1 Reply)
Discussion started by: Neo
1 Replies

2. Shell Programming and Scripting

Append 0's based on length

I'm having data like this, "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"... (3 Replies)
Discussion started by: Artlk
3 Replies

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

4. Linux

Bash to append domain in search field of resolv.conf after vpnc connection in Linux

After vpn connection,I am not able to resolve any machines in remote gateway.It looks like remote domain is added to domain field instead of adding it to the Search field in /etc/resolv.conf I want the remote domain to add to search field along with local domain.Can anyone tell a bash or script... (2 Replies)
Discussion started by: jeremy_brett
2 Replies

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

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

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

8. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

9. Shell Programming and Scripting

Help with finding length of a field

I have a pipe delimited file. I need to check that the first and second fields are 5 characters long and if not i need to append 0 in front of them to make them 5 characters long. can some body let mwe know how i can find the length of the two fields and then make them 5 characters long if they... (6 Replies)
Discussion started by: dsravan
6 Replies
Login or Register to Ask a Question