in reply to Running complex sed from perl

Assuming the regex conversion was correct. Would the following serve?

#!/bin/perl -p s#\D*(\d{3})\D*(\d{3})\D*(\d{4})#(${1}) ${2}-${3}#s;

sample input

adhjfgl 123ab786YT789034 asdf123ab786YT789034 (123) 786-789034 123ab786YT7890bc 1234567890

output

adhjfgl (123) 786-789034 (123) 786-789034 (123) 786-789034 (123) 786-7890bc (123) 456-7890

Replies are listed 'Best First'.
Re^2: Running complex sed from perl
by sunglant (Initiate) on Dec 02, 2013 at 02:03 UTC

    here is the code that I used, to test based on the the information given, I used a perl one liner to validate it works without a hitch

     perl -pi.back -e 's#\D*(\d{3})\D*(\d{3})\D*(\d{4})#(${1}) ${2}-${3}#s;' clients.txt

    Now of course, I realized the flaw in my thinking, and several components of the script are completely written on Linux systems, the flaw of course is that when testing the perl script on a windows system without say cgywin and only strawberry or absolute perl calls for awk and sed will not work. Luckly at this point there is only a single awk command left. All it does is place double quotes around each line in the output.txt file so that the perl script can then modify the other lines with the necessary formating. If I can figure out how to get double quotes at the begining and end of each line, then when its formated it can be converted into a CSV file and imported into a DB.

      If I can figure out how to get double quotes at the begining and end of each line...

      There are several ways to do that.

      # if the current line is in $_ s/^|$/"/g; # if the current line is in $line $line =~ s/^|$/"/g; # or with [doc://substr] substr $_, 0, 0, '"'; substr $_, length $_, 0, '"'; # but it is simpler just to append a " $_ .= '"'; # or even $_ = '"' .$_ . '"';
      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

        I attempted this and I get a double quote in front of each line, that was one of the issues I was having with the perl componet, which was I returned to awk. Do you have any idea why only the beginning lines get two double quotes? The same thing occurs when I attempted to make a one liner out of the sed call for the double quotes.

      Hi sunglant,

      If I can figure out how to get double quotes at the begining and end of each line, then when its formated it can be converted into a CSV file and imported into a DB.
      I really don't know why you are trying to code in sed and awk using perl.
      Since perl can do want you want why not write all your script in perl altogether? Then using a module like Text::CSV or Text::CSV_XS you can get your dataset in csv then getting all of that into your preferred DB still using perl.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

      There is more than one way to do it. You could add double quotes via additional search and replace.

      perl -pi.back -e 's#\D*(\d{3})\D*(\d{3})\D*(\d{4})#(${1}) ${2}-${3}#s; + s#^#""; s#$#"#' clients.txt
Re^2: Running complex sed from perl
by sunglant (Initiate) on Dec 02, 2013 at 00:42 UTC

    That works beautifully, I am more than thank full. Which lead to a different problem in the script, now my awk line isn't working. I assume the issue is that the system I am working on doesn't have cgywin installed. Anyone have a quick perl one liner to put quotes at the begining of a line then the end? thank you all very much, to me being a novice with varying languages and limited scripting skills leaves alot of this up to being simply black majik, and Perl has many functions in blackboxes which I don't fully understand.

Re^2: Running complex sed from perl
by sunglant (Initiate) on Dec 01, 2013 at 22:35 UTC

    Thank you, I will run some testing this evening, and then will respond again.