Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

perl one-liner, is it possible?

by hxwu (Initiate)
on Feb 07, 2006 at 07:33 UTC ( [id://528439]=perlquestion: print w/replies, xml ) Need Help??

hxwu has asked for the wisdom of the Perl Monks concerning the following question:

I have one ldif file, which is a dump file from Oracle OID. Many lines got cut into 2 lines due to 80 columns limit, not sure why. So there are many similar lines as follows in the file:
cn: ias_admin
creatorsname: orclapplicationcommonname=uddi,cn=portal,cn=products,cn=oracleco
 ntext
modifiersname: orclapplicationcommonname=uddi,cn=portal,cn=products,cn=oraclec
 ontext
description: UDDI Registry Administrator. It maps to the ias_admin user of Ent
 erprise Manager.
I want to change the above to below:
cn: ias_admin
creatorsname: orclapplicationcommonname=uddi,cn=portal,cn=products,cn=oraclecontext
modifiersname: orclapplicationcommonname=uddi,cn=portal,cn=products,cn=oraclecontext
description: UDDI Registry Administrator. It maps to the ias_admin user of Enterprise Manager.
Is it possible for perl one-liner?

Replies are listed 'Best First'.
Re: perl one-liner, is it possible?
by strat (Canon) on Feb 07, 2006 at 09:32 UTC

    the 80 chars are specified in an RFC, but I can't remember which one

    You can read it object by object, i.e. not \n as a input record separator, but \n\n....

    my $LDIF; unless (open ($LDIF, "<", $ldifFile)) { die "Error in reading '$ldifFile': $!\n"; } else { local $/ = "\n\n"; # set input record separator to \n\n while (my $object = <$LDIF>) { chomp($object); $object =~ s/\n //g; # kill continuation lines print $object, "\n\n"; } # while close ($LDIF); } # else

    well, the chomp and the print ... "\n\n" are not really necessary in this case

    Or use Net::LDAP::LDIF for a better solution

    or as a one-liner for windows:

    perl -e "$/ = qq~\n\n~" -pe "s/\n /g" infile > outfile

    or for unix/linux:

    perl -e '$/ = "\n\n"' -pe 's/\n /g' infile > outfile

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: perl one-liner, is it possible?
by BrowserUk (Patriarch) on Feb 07, 2006 at 08:03 UTC

    If the tail of the split lines always starts with a space (that should be removed), and no other lines do, then this maybe close (wrapped for posting):

    perl -nle "if(m[^\s(.*)]){$s.=$1}else{length($s)&print$s;$s=$_;next};END{print$s +}" file >out

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: perl one-liner, is it possible?
by olus (Curate) on Feb 07, 2006 at 12:11 UTC
    Sure!
    perl -i -e '$/=\32000; while(<>) {s/\s{2}//sg;print $_;}' oid.ldif

    Change the input record separator, so that instead of the reading of the file breaks on new lines, we read chunks of data.
    Next, just substitute the two white space characters ('\n' and ' ') with nothing and the lines get concatenated.

    --
    olus
      perl -pi -e 'BEGIN { $/=\32000 } s/\s{2}//sg' oid.ldif


      Mago
      mago@rio.pm.org


Re: perl one-liner, is it possible?
by izut (Chaplain) on Feb 07, 2006 at 09:14 UTC
    If the file is not too big you can read entire file and then join the lines, like this:
    perl -e '@_ = <STDIN>; $_=join("", @_); s/\n\s{1}//gm; print'

    Igor 'izut' Sutton
    your code, your rules.

Re: perl one-liner, is it possible?
by Roy Johnson (Monsignor) on Feb 07, 2006 at 15:44 UTC
    perl -0040 -pe "s/\n //" file.txt
    Set the input record separator to space, then remove any newline-space combinations.

    Caution: Contents may have been coded under pressure.
Re: perl one-liner, is it possible?
by CountOrlok (Friar) on Feb 07, 2006 at 15:40 UTC
    If the file is not too large:
    perl -0777 -pe 's/\n\s//g' inputfile
    -imran

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://528439]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-18 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found