How to parse a numeric string without any delimiters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to parse a numeric string without any delimiters?
# 1  
Old 11-06-2011
How to parse a numeric string without any delimiters?

Hi ,

I have a number say 12345001 which needs to be parsed. Its a number that has no delimiters.I have to read the last three digits and then the rest of digits irrespective of the total length of the number. The digits then have to be swapped and changed to a fixed length. The fillers to be inserted are X.

In short the input is 12345001 and the output needed is 001XX12345. The total length has to be 10.

Please suggest a solution which is performance oriented because the input has to be read from a file that would have around 50K records in it.

TIA.
# 2  
Old 11-06-2011
Code:
awk '{
  f = sprintf("%*-s", l - length + 3, substr($0, length - 2)) 
  gsub(/ /, c, f)
  print  f substr($0, 1, length - 3)
  }' l=10 c=X infile

# 3  
Old 11-07-2011
Unfortunately i can not use gsub as its not supported. The command returned
Quote:
-s112233
for input
Quote:
112233001
Smilie

Please suggest an alternative.
# 4  
Old 11-07-2011
Quote:
Originally Posted by Sheel
Unfortunately i can not use gsub as its not supported.
Are you on Solaris? Use nawk.
# 5  
Old 11-07-2011
The problem is with gsub which is not installed . I am using AIX. Thanks.
# 6  
Old 11-07-2011
gsub is part of awk. Exactly what error are you getting when you run radoulov's script?
# 7  
Old 11-07-2011
There is no error. The script works just fine but the output expected is not returned. Can you explain the logic pls.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace string between delimiters?

Hello, I would need to replace a delimiter in a flat file using.I would like to replace the semicolon (";") but only if it was contained in a string between quotes. For example: Original flat file example: abc;abc;"abc;abc";cd;"ef;ef";abc aa;bb;"aa";cc;"ddd;eee";ff Desired output:... (9 Replies)
Discussion started by: bartleby
9 Replies

2. Shell Programming and Scripting

How to Replace string between delimiters?

Basically , i want to delete strings of a particular pattern from the flat file which is " | " pipe delimited. Below are the valid formats : 1) AAA (0) 111-111-111, AAA, BB 2) AAA (0) 111-111-1111;X, AAA, BB original flat file example : |ABC ABC XHAMK|AAA (0) 111-111-111, AAA,... (3 Replies)
Discussion started by: Ravi_007
3 Replies

3. Shell Programming and Scripting

check if a string is numeric

I checked all the previous threads related to this and tried this. My input is all numbers or decimals greater than zero everytime. I want to check the same in the korn shell script. Just validate the string to be numeric. This is what I am doing. var="12345" if ) -o "$var" !=... (14 Replies)
Discussion started by: megha2525
14 Replies

4. Shell Programming and Scripting

Getting a string without fixed delimiters

I have a line of text for example aaaa bbbb cccc dddd eeee ffffff I would need to get the cccc however bbbb could be there or not. So whether bbbb is in the line or not I need cccc. I was looking at either awk or sed....and trying to start at c and end until the next space. Also... (11 Replies)
Discussion started by: bombcan1
11 Replies

5. UNIX for Dummies Questions & Answers

Delete string between delimiters with sed

Hi, I need to delete all text between "|" delimiters. The line in text file typically looks like this: 1014182| 13728 -rw-r--r-- 1 imac1 staff 7026127 2 okt 2010 |/Users/imac1/Music/iTunes/iTunes Media/Music/Various Artists/We Are the World_ U.S.A. for Africa/01 We Are the World.mp3... (2 Replies)
Discussion started by: andrejm
2 Replies

6. Shell Programming and Scripting

parse apl-numeric codes from filenames, and match them to entries in database

Hello, I am new to Unix scripting, and would like some help with my issue: I have vairous files having some alphanumeric codes in them e.g. 10000-01 34440TE 34590SR All these codes are stored in the database, and I need to parse these codes out of these filenames, and match them... (2 Replies)
Discussion started by: mvaidya
2 Replies

7. Shell Programming and Scripting

sub-string extract between variable delimiters

I need to extract certain pieces from a string, wher delimiters may vary. For example A0 B0 C0 12345677 X0 Y0 Z0 A1-B1 C1 12345678 X1 Y0 Z0 A1/B2 C77 12345679 X2 Y0 Z0 I need to get C0 12345677 X0 C1 12345678 X1 C77 12345679 X2 I tried sed, see example below: echo 'A0 B0... (2 Replies)
Discussion started by: migurus
2 Replies

8. Shell Programming and Scripting

string deletion, variable contents, fixed delimiters

Hi, I need to mass delete the following string(s) from my files weight=100, However the '100' is variable e.g Current: ---------------- moretext=bar, weight=100, moreinfo=blah extrastuff=hi, weight=9999, extrainfo=foo Desired: ------------------ moretext=bar, moreinfo=blah... (2 Replies)
Discussion started by: rebelbuttmunch
2 Replies

9. Shell Programming and Scripting

parse of lines with different delimiters

Hi, I am having huge file with the following lines. 2007:10:01:00:00:49:GMT: subject=BMRA.BM.T_ABTH7.FPN, message={SD=2007:10:01:00:00:00:GMT,SP=5,NP=2,TS=2007:10:01:01:00:00:GMT,VP=0.0,TS=2007:10:01:01:30:00:GMT,VP=0.0} 2007:10:01:00:00:49:GMT: subject=BMRA.BM.T_ABTH7G.FPN,... (9 Replies)
Discussion started by: nathasha
9 Replies

10. Shell Programming and Scripting

Using sed to delete string between delimiters

Hi There! I have the following string which i need to convert to i.e. between each occurence of the delimiter ('|' in this case), i need to delete all characters from the '|' to the ':' so that |10,9:12/xxx| becomes |12/xxx| How can i do this using sed? Thanks in advance! (13 Replies)
Discussion started by: orno
13 Replies
Login or Register to Ask a Question