Awk adding a space between integer and the rest of the field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk adding a space between integer and the rest of the field
# 1  
Old 10-08-2008
Awk adding a space between integer and the rest of the field

Hi,

I have a problem where some of the records I need to process have the first address field as something like "10Walpole Street" where obviously I want it to be "10 Walpole Street". I know I need to somehow separate out the integer and probably form a new string variable, but I just don't know how to do it.

There are other instances where there may be a ";" stuck in there instead of a space too, futher into the address or in the next address field, etc. Thinking about this latter case, I should be able to add ";" to the field separator list. Smilie

I'm still left with some other badly formed addresses that sometimes contain no space in the middle where there probably should be a field separator or space, for example "scotlanwellgreen,stirling", I don't know if I could hope to catch all such occurances, and I don't even know for sure that such place names are supposed to have spaces! Looking up "scotlanwell green" on google suggests that I might try "Scotlandwell green", which turns out to be a real place - it's just another badly typed or formed address that I probably have no hope of catching and correcting in a script. The occurances are seemingly random.

Any help with these would be much appreciated!
# 2  
Old 10-08-2008
Quote:
Originally Posted by jonathanm
I have a problem where some of the records I need to process have the first address field as something like "10Walpole Street" where obviously I want it to be "10 Walpole Street". I know I need to somehow separate out the integer and probably form a new string variable, but I just don't know how to do it.

Replace $0 with the correct field:

Code:
awk '/[0-9][A-Z]/ {
    n = index($0, /A-Z/)
    num = substr($0, 1, n)
    addr = substr($0, n + 1 )
    print num, addr
 }'

# 3  
Old 10-08-2008
Hammer & Screwdriver a unix script solution

Code:
> echo "$street"
10Walpole Street
> snum=$(echo $street | cut -d" " -f1 | tr -d "[:alpha:]" )
> sadd=$(echo $street | cut -d" " -f1- | tr -d "[0-9]")
> echo ${snum}" "${sadd}
10 Walpole Street
>

# 4  
Old 10-08-2008
Quote:
Originally Posted by joeyg
Code:
> echo "$street"
10Walpole Street
> snum=$(echo $street | cut -d" " -f1 | tr -d "[:alpha:]" )
> sadd=$(echo $street | cut -d" " -f1- | tr -d "[0-9]")
> echo ${snum}" "${sadd}
10 Walpole Street
>


If you have the address in a shell variable, you don't need 4 external commands to process it; you don't need any:

Code:
string="10Walpole Street"
street=${string##*[!A-Za-z ]}
num=${string%"$street"}
printf "%s %s\n" "$num" "$street"

# 5  
Old 10-08-2008
what if string="10W2 Street" ?
The upon script won't work well.
string="10w3c street"
num=${string%%[A-Za-z ]*}
street=${string##"$num"}
printf "%s %s" "$num" "$street"

Last edited by a2156z; 10-09-2008 at 12:06 AM..
a2156z
# 6  
Old 10-09-2008
Quote:
Originally Posted by a2156z
what if string="10W2 Street" ?
The upon script won't work well.
string="10w3c street"
num=${string%%[A-Za-z ]*}
street=${string##"$num"}
printf "%s %s" "$num" "$street"

The output of that script is:

Code:
10 w3c street

Or, with the other string:

Code:
10 w2 street

Isn't that what is wanted?

If not, then the problem was not adequately specified (and the only answer may be to open a text editor and fix the file manually).

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Concatenate integer and space/newline for cout in C++

I'm trying to print out integers and space/newline for a nicer output, for example, every 20 integers in a row with ternary operator. In C I could do it with:printf("%d%s",tmp_int, ((j+1)%20) ? "\t":"\n"); but could not figure out the equivalent in C++: cout << ((j+1)%20)?... (4 Replies)
Discussion started by: yifangt
4 Replies

2. Web Development

Javascript to check field is empty then execute rest of script

I have found this bit of code that nearly does what I want. Basically 3 input fields, I want to copy t2 to t3 as it's typed but only if t1 contains data AND t3 is empty: <input type="text" id="t1" /> <input type="text" id="t2" /> <input type="text" id="t3" /> <script> var t2 =... (4 Replies)
Discussion started by: barrydocks
4 Replies

3. Shell Programming and Scripting

Nth field should have space and rest commas

Hello I was working on a script where the output of my file is 1234 4567 8973 43214 78965 I need the value in below format of this file.The nth field should have space instead of ,(comma) 1234,4567,8973,43214 78965 I tried the code but not working completely xargs <temp_PP.7250... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

4. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

5. Shell Programming and Scripting

awk - trim white space from a field / variable

Hi, Consider the data (FS = |): 1| England |end 2| New Zealand |end 3|Australia|end 4| Some Made Up Country |end 5| West Indies|end I want the output to be (i.e. without the leading and trailing white space from $2) England New Zealand Australia Some Made Up Country West... (4 Replies)
Discussion started by: Storms
4 Replies

6. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

7. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

8. Shell Programming and Scripting

awk get rid of space in end of field

Hello. I'm using a file to "grep" in a 2nd one (with awk) cat file1 2 first user 9 second user 1 third user (with a space after user) I want to get the line except the 1st field so I do : field=$(gawk '{$1 =""; print $0}' file | sed 's/^ //') It works but it deletes... (5 Replies)
Discussion started by: xanthos
5 Replies

9. UNIX for Dummies Questions & Answers

Multiple field separators in awk? (First a space, then a colon)

How do I deal with extracting a portion of a record when multiple field separators are involved. Let's say I have: Mike Harrington;(555) 555-5555:250:100:175 Christian Dobbins;(555) 555-2358:155:90:201 Susan Dalsass;(555) 555-6279:250:60:50 Archie McNichol;(555) 555-1348:250:100:175 Jody... (3 Replies)
Discussion started by: doubleminus
3 Replies

10. UNIX for Dummies Questions & Answers

Adding a new field using sed or awk?

I have a bcp file that contains 10 fields. These fields are separated by a tab. How can I add my name as a new field in the 8th position for every record? I've been playing w/ sed and awk but can't seem to figure this out. (3 Replies)
Discussion started by: sasabune
3 Replies
Login or Register to Ask a Question