Inserting a space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting a space
# 1  
Old 02-27-2004
Inserting a space

I am trying to reformat some UK postal codes. I have a csv where field 12 is the postal code. The postal codes length has a maximum of 7.
Basically, I would like a bit of code to look at field 12 and if the postal code has a length of 7, then insert a space into the field fourth from the right, i.e. before - BB102EE, after - BB10 2EE

Help me
# 2  
Old 02-27-2004
i feel kinda retarted for haveing to use 2 substr but i cant think of another way to do the inline edit of $2 at the moment.

Code:
#!/usr/bin/perl -w

use strict;

while (<>) {
if (/(\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,)(\w*)(.*)/) {
        if (length($2) == 7) {
                my $zipstart=substr($2,0,4);
                my $zipend=substr($2,4,3);
                print "$1$zipstart $zipend$3\n";
                next;
        }

        print "$1$2$3\n";
}
}

The below is what the perl group said.

Code:
When working with long, delimited lists of data like
that, I'm inclined to split it, twiddle it, then
reassemble it.

while (<>) {
 my @data = split /,/;
 #use substr to substitute in a space at the 4th
character if our string is 7 characters long
 substr($data[11], 4, 0, " ") if length $data[11] ==
7;
 print join(',', @data); #no need for a newline, we
have one at the last position
}

if you want, you could use $, set to ',' and then just
print @data at the end. Might be a little faster, but
I prefer being explicit.

That should work, but I didn't test it, so it may need
a little twiddling.

But you also caught my curiousity...what's with a 7
digit zipcode and what would the space do? I'm only
familiar with 5 digit, 9 digit, and 11 digit (DPBC)
codes (and none of those have spaces). I'm thinking
canadian postal codes, but I'm not sure.

-Jim......


Last edited by Optimus_P; 02-27-2004 at 12:23 PM..
# 3  
Old 02-27-2004
Using awk...
Code:
awk '
BEGIN{FS=OFS=","}
{
  if (length($12)==7) {
    $12=substr($12,1,4) " " substr($12,5,3);
  }
  print $0;
}
' file1

# 4  
Old 02-27-2004
Thanks very much for your responses. It's been a great help.

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk --- produce the output in pattern space instead of END space

hi, I'm trying to calculate IP addresses and their respective calls to our apache Server. The standard format of the input is HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST... (2 Replies)
Discussion started by: busyboy
2 Replies

2. UNIX for Advanced & Expert Users

Need to remove leading space from awk statement space from calculation

I created a awk state to calculate the number of success however when the query runs it has a leading zero. Any ideas on how to remove the leading zero from the calculation? Here is my query: cat myfile.log | grep | awk '{print $2,$3,$7,$11,$15,$19,$23,$27,$31,$35($19/$15*100)}' 02:00:00... (1 Reply)
Discussion started by: bizomb
1 Replies

3. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

4. Fedora

Need to incrwase PHYSICAL VOLUME space on hard drive with free space on it

Hi, I run Fedora 17. I created a physical volume of 30GB on a disk with 60GB of space so there is 30GB of free space. On the physical volume, I created my volume group and logical volumes. I assigned all the space in the physical volume to my volume group. I need to add the 30GB of free space... (1 Reply)
Discussion started by: mojoman
1 Replies

5. Solaris

No space left on device but free space and inodes are available...

hi guys, me again ;) i recently opened a thread about physical to zone migration. My zone is mounted over a "bigger" LUN (500GB) and step is now to move the old files, from the physical server, to my zone. We are talking about 22mio of files. i used rsync to do that and every time at... (8 Replies)
Discussion started by: beta17
8 Replies

6. Linux

How to reclaim the space which i used to increse the swap space on Xen,

Hi, i have done a blunder here, i increased the swap space on Xen5.6 server machine using below steps :- 1056 dd if=/dev/zero of=/root/myswapfile bs=1M count=1024 1057 ls -l /root/myswapfile 1058 chmod 600 /root/myswapfile 1059 mkswap /root/myswapfile 1060 swapon /root/myswapfile ... (1 Reply)
Discussion started by: apm
1 Replies

7. UNIX for Dummies Questions & Answers

Inserting space between numbers

Are there any one-liners or short codes to separate 4-digit numbers into 2? For example, input.txt: 1234 5678 3091 2851 Output.txt: 12 34 56 78 30 91 28 51 (7 Replies)
Discussion started by: pxalpine
7 Replies

8. Shell Programming and Scripting

Stripping out more than a space from a line, but keep single space.

Hi all, Is there a way to perform the above, I am trying to strip out more than one space from a line, but keep the single space. See below output example. My Name is test test2 test3 test4 test5 My Name is test test2 test3 test4 test5 Please note that the lines would contain... (7 Replies)
Discussion started by: eo29
7 Replies

9. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

10. UNIX for Advanced & Expert Users

wake up user space thread from kernel space ISR

Hello, I'm searching for a proper way to let the kernel space ISR(implemented in a kernel module) wake up a user space thread on a hardware interrupt. Except for sending a real-time signal, is it possible to use a semaphore? I've searched it on google, but it seems impossible to share a... (0 Replies)
Discussion started by: aaronwong
0 Replies
Login or Register to Ask a Question