The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to split special characters "|" using awk krishna9 Shell Programming and Scripting 3 05-22-2008 06:30 AM
perl split funciton - special character "/" deepakwins UNIX for Dummies Questions & Answers 5 02-08-2008 12:19 AM
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
split the string "Setview: arv-temp-view" amitrajvarma Shell Programming and Scripting 2 10-11-2007 05:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-29-2007
lumdev lumdev is offline
Registered User
  
 

Join Date: Sep 2006
Location: Belgium
Posts: 6
Perl - split with "|"

Hi,

I’m having some troubles with the split-command. My file is formatted like this:

field1 | field2 | field3 | field4 | …

The purpose is to catch for example the first and fourth field so I’m using “|” as delimiter, but the thing is that he then only takes the first character out of the field. So for example when I have the following:

abc | def | ghi | jkl | …

With the script below I’m getting:

a b c d in stead of abc def ghi jkl

Now if for instance I use “#” as delimiter I get the correct output. So now I’m wondering why he doesn’t execute the split correctly with the “|” delimiter?


Code:
#!/usr/bin/perl

opendir(DIR,".");

@contents = readdir(DIR);

closedir(DIR);

foreach $file (@contents)
{
  if ($file =~ m/test.txt/)
  {
    open(FILE,$file);

    while($line = <FILE>)
    {
      chomp($line);
      (@params) = split('|',$line);

      foreach $p (@params)
      {
        print "$p\n";
      }

    }
  }
}
  #2 (permalink)  
Old 01-29-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
Code:
#!/usr/bin/perl

opendir(DIR,".");

@contents = readdir(DIR);

closedir(DIR);

foreach $file (@contents)
{
  if ($file =~ m/test.txt/)
  {
    open(FILE,$file);

    while($line = <FILE>)
    {
      chomp($line);
      (@params) = split(/\|/,$line);

      foreach $p (@params)
      {
        print "$p\n";
      }

    }
  }
}
because the delimiter is treated as a regexp, and | has special meaning in regexp which needs to be escaped as shown.
  #3 (permalink)  
Old 01-29-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Add a backslash
Code:
(@params) = split('\|',$line);
  #4 (permalink)  
Old 02-04-2007
lumdev lumdev is offline
Registered User
  
 

Join Date: Sep 2006
Location: Belgium
Posts: 6
Thx, it works fine now.
Sponsored Links
Closed Thread

Bookmarks

Tags
delimited, perl split

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0