Perl split string separated by special character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl split string separated by special character
# 1  
Old 02-24-2015
Perl split string separated by special character

Hello

I have string (string can have more sections)
Code:
LINE="AA;BB;CC;DD;EE"

I would like to assigne each part of string separated by ";" to some new variable.

Can someone help?
# 2  
Old 02-24-2015
This is especially convenient in perl as you can assign a list of variables to a list of results. split() like many things returns a list.

Code:
my ($a,$b,$c,$d,$e)=split(/;/,"a;b;c;d;e");

print "a is $a\n";
print "b is $b\n";

It won't arbitrarily declare new varibles, though. For an indeterminate number of results, use a list.
Code:
my @list=split(/;/, "a;b;c;d;e");

print @list[0], "\n";

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-24-2015
Hello vikus,

Not sure if I completly understood requirement, but could you please try following with awkand let me know if this helps.
Code:
echo $LINE | awk -F";" '{for(i=1;i<=NF;i++){print "VAR"_++j "=" $i}}

Output will be as follows. As of now it is doing print only we can use the same as variables too.
Code:
VAR0=AA
VAR1=BB
VAR2=CC
VAR3=DD
VAR4=EE

Following is the example for using like a variable.

Code:
echo $LINE | awk -F";" '{for(i=1;i<=NF;i++){A="VAr_"++j;;if(A=="VAr_3"){print $i}}}'

Output will be as follows.
Code:
CC


Thanks,
R. Singh
# 4  
Old 02-24-2015
Code:
my @list=split(/;/, "a;b;c;d;e");

foreach my $val (@list) {
    print $val ;
}

I have someting like this, how to assigned to value this what is printed?
# 5  
Old 04-09-2015
It already is assigned to something, otherwise you couldn't print it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging two special character separated files based on pattern matching

Hi. I have 2 files of below format. File1 AA~1~STEVE~3.1~4.1~5.1 AA~2~DANIEL~3.2~4.2~5.2 BB~3~STEVE~3.3~4.3~5.3 BB~4~TIM~3.4~4.4~5.4 File 2 AA~STEVE~AA STEVE WORKS at AUTO COMPANY AA~DANIEL~AA DANIEL IS A ELECTRICIAN BB~STEVE~BB STEVE IS A COOK I want to match 1st and 3rd... (2 Replies)
Discussion started by: crypto87
2 Replies

2. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

3. Shell Programming and Scripting

How to replace with a special character in String

Hi, I am beginner to Shell Scripting. I have a String like this "testabcdef", i need the first character as it is and the remaining character should be replaced by the the '*' character. e.g(t***********) PLZ Suggest me. (5 Replies)
Discussion started by: nanthagopal
5 Replies

4. Shell Programming and Scripting

Unix Perl split special character $

All I'm trying to split a string at the $ into arrays @data:=<dataFile> a $3.33 b $4.44 dfg $0.56 The split command I have been playing with is: split(/\$/, @data) which results with a .33 b .44 dfg .56 any help with this is appreciated /r Rick (9 Replies)
Discussion started by: schultz2146
9 Replies

5. UNIX for Advanced & Expert Users

Escaping special character stored in variables : perl

Hi just for regular use i m working on small module written in perl for getting date in specified format like i have to specify date format and then seperator to seperate date i am 95% done. now i m sure explanation i gave is not good enough so i am putting output here : C:\Documents and... (2 Replies)
Discussion started by: zedex
2 Replies

6. Shell Programming and Scripting

Remove box like special character from end of string

Hi All, How to remove a box like special character which appears at the end of a string/line/record. I have no clue what this box like special character is. It is transparent square like box. This appears in a .DAT file at the end of header. I'm to compare a value in header with a parameter.... (16 Replies)
Discussion started by: Qwerty123
16 Replies

7. Shell Programming and Scripting

Perl Script Syntax to Extract Everything After Special Character

Hi, I am writing a Perl script that reads in many lines, if a line meets the criteria I want to edit, it. For example, the script will return the following example line... test=abc123 All I want to do is strip off the "test=" and just be left with the abc123. In my script I can easily... (3 Replies)
Discussion started by: edrichard
3 Replies

8. UNIX for Dummies Questions & Answers

perl split funciton - special character "/"

HI, I have a directory structure. /abc/def/ghi/ I want to store it into array. So that if I do a pop function on that array I can easily go to previous directory. But how can i split and store it. @Directory = split(/\//,$DirectoryVarialbe) That doest works. Any other escape sequence... (5 Replies)
Discussion started by: deepakwins
5 Replies

9. Shell Programming and Scripting

Need help to extract a string delimited by any special character

I have a string as follows IS*blahblah TED~blahblah etc. I want to list down only IS and TED Can someone help me? (24 Replies)
Discussion started by: kumariak
24 Replies

10. Shell Programming and Scripting

replacing string with special character ???

the problem is while replacing the old string with new one with the help of SED i am unable to replace the special characters with new strings. how can i do that? i dont want the user to be given the trouble to write '\' before every special characters like * , . , \ , $ , &. sed... (4 Replies)
Discussion started by: imppayel
4 Replies
Login or Register to Ask a Question