From owner-freebsd-isp Mon May 14 8: 7:18 2001 Delivered-To: freebsd-isp@freebsd.org Received: from tacni.net (mail.tacni.net [216.178.136.165]) by hub.freebsd.org (Postfix) with SMTP id 680B737B42C for ; Mon, 14 May 2001 08:07:14 -0700 (PDT) (envelope-from tom.oneil@tacni.com) Received: (qmail 61446 invoked by alias); 14 May 2001 15:07:11 -0000 Received: from unknown (HELO tacni.com) (216.201.173.186) by pendragon.tacni.net with SMTP; 14 May 2001 15:07:11 -0000 Message-ID: <3AFFF4A2.EEEBB0CF@tacni.com> Date: Mon, 14 May 2001 10:07:14 -0500 From: Tom ONeil Organization: Texas American Communications X-Mailer: Mozilla 4.77 [en] (WinNT; U) X-Accept-Language: en MIME-Version: 1.0 To: Free Subject: sed question - the fix. Many thanks to all! Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-isp@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org I received so many great responses I thought I'd cat them all together in one post. Many mega thanks to all. The Question; Greetings All; I have a script that handles our pop numbers and it requires I change the format of a large poplist we use for input. This is the format - CA,Anaheim,714,408,4411 and what I need is - CA,Anaheim,714-408-4411 because I don't enough perl to modify the script and I can't figure out how use sed to do it. awk will do the substitution, but removes the first 2 commas in the process. awk -F, '{print $1,$2,$3,-$4,-$5}' poplist Thoughts? snippets of code? Tom Glenn foster awk -F , '{print $1","$2","$3"-"$4"-"$5}' Rowan Crowe awk -F, '{print $1 "," $2 "," $3 "-" $4 "-" $5}' poplist Andy #!/usr/local/bin/perl while () { ($a,$b,$c,$d,$e) = split /,/; print "$a,$b,$c-$d-$e"; } then do:- $ cat oldfile | prog > newfile where "prog" is the name of the perl script holding those lines at the start of this email. Regards Andy #!/usr/bin/perl use strict; my $file = "/patch/to/database"; open FILE, $file; while () { chomp; my @arr = split(/([A-Z]+)\,([a-zA-z]+)\,(\d+)\,(\d+)\,(\d+)/o, $_); shift(@arr); print $arr[0] . "," . $arr[1] . "," . $arr[2] . "-" . $arr[3] . "-" . $arr[4] . "\n"; } close FILE Best regards, Alexei V. Alexandrov awk -F, '{print $1 "," $2 "," $3 "-" $4 "-" $5}' < poplist Enno. bash-2.05# cat blah CA,Anaheim,714,408,4411 bash-2.05# cat blah | sed 's/\([0-9]\),\([0-9]\)/\1-\2/g' CA,Anaheim,714-408-4411 it matches a "," surrounded by numbers and replaces the comma with a dash. Hope this helps. andys -- Thomas J. ONeil tom.oneil@tacni.net http://www.tacni.net/ "National power, local presence" To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-isp" in the body of the message