How to cut fields with pipe separator in a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jayakumarrt
    New Member
    • Jun 2007
    • 10

    How to cut fields with pipe separator in a file

    Dear friends,


    Inside the perl script i will opening the file which is having three columns with pipe separated.

    a|1|abc
    b|2|bcd
    c|3|one

    Now i want to read the first record (a|1|abc), split the record and assign the each column values to some variable.
    Can anybody help me in this ?

    Thanks in advance
    Jayakumar
  • prn
    Recognized Expert Contributor
    • Apr 2007
    • 254

    #2
    Hi jayakumarrt,

    See the documentation on the split function for how to do this.

    Best Regards,
    Paul

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      Greetings jayakumarrt,

      As Paul said, simply use the split function. Just note that the pipe | is a regex meta character and therefore must be escaped to use properly.

      [CODE=perl]
      my $s = 'a|1|abc';
      my @a = split '\|', $s;
      print join ',', @a; # Outputs a,1,abc
      [/CODE]
      - Miller

      Comment

      • jayakumarrt
        New Member
        • Jun 2007
        • 10

        #4
        Thanks Miller and Paul

        Comment

        Working...