Perl: Separator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Separator
# 1  
Old 07-15-2009
Perl: Separator

Hello all,

I'm trying to break down a file with the following format:

[section_nme]
entries
dzdf
daff

[section 2]
enries
dfln
fnljfd
.
.
..
.


I'm reading this file, using "[" as my separator. It's working quite well, but I would like to be able to read the "[" character into my array as a valid character as well. So I'm wondering - is it possible to set a "just before" tag as my separator ($/)??

Thanks.

Last edited by fpmurphy; 07-15-2009 at 11:03 AM.. Reason: Added missing word "file' to first sentance
# 2  
Old 07-15-2009
As far as I know it is not possible. $/ is just a simple string, you can't try and define a pattern like you might be able to do with other shells scripts. But you can add the '[' as needed to the output after getting it from the file.
# 3  
Old 07-15-2009
don't use $/

try reading whole thing as a string, use split to get whwat you need instead

$/ is for cutting streams. It isn't even a string until you place it into a register.
or use it to slurp the whole file into a variable.

Example

open (FILE, "$filename");
local $/;
$file=<FILE>;
close(FILE);

#the entire file is in one variable including the [
@file=split("[", $file);
#now it is in an array.
# 4  
Old 07-15-2009
Quote:
Originally Posted by sighK
don't use $/

try reading whole thing as a string, use split to get whwat you need instead

$/ is for cutting streams. It isn't even a string until you place it into a register.
or use it to slurp the whole file into a variable.

Example

open (FILE, "$filename");
local $/;
$file=<FILE>;
close(FILE);

#the entire file is in one variable including the [
@file=split("[", $file);
#now it is in an array.
I am pretty sure that is going to do the exact same thing he is already doing.
# 5  
Old 07-15-2009
You may want to adopt a different approach in lieu of splitting -

Code:
$
$ cat data.txt
[section_nme]
entries
dzdf
daff

[section 2]
enries
dfln
fnljfd

[section 3]
entry_1
entry_2
entry_3

$ 
$ perl -ne 'BEGIN{$/=""}{while (/(\[.*)/sg) {print ">>",$1}}' data.txt
>>[section_nme]
entries
dzdf
daff

>>[section 2]
enries
dfln
fnljfd

>>[section 3]
entry_1
entry_2
entry_3

$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separator

Hello everybody, I'll get one more help I have a cabundle file that I need to separate into 2 parts, the first sequence and the second sequence, I thought of several things but I did not remember anything that could actually accomplish this separation and transform into 2 variables, first... (4 Replies)
Discussion started by: c0i0t3
4 Replies

2. Shell Programming and Scripting

Awk, with separator |

Friends have the following code that is correct. BEGIN { num_reg = 0 suma_iva=0 } { num_reg++ suma_iva=suma_iva+int(substr($0, 103,9)) } END{ printf ("%011d",suma_iva) } I have the following problem, I have to do just that but this time... (4 Replies)
Discussion started by: tricampeon81
4 Replies

3. Shell Programming and Scripting

Field separator

Hello All, I have a file, but I want to separate the file at a particular record with comma"," in the line Input file APPLE6SSAMSUNGS5PRICEPERPIECEDOLLAR600EACH010020340URX581949695US to Output file APPLE6S,SAMSUNGS5,PRICEPERPIECE,DOLLAR600EACH,010020340URX581949695,US This is for... (11 Replies)
Discussion started by: m6248m
11 Replies

4. Shell Programming and Scripting

Row separator

Hello All, I need help with the below, I would appreciate any tip. I have a file as below Input file Apple: Green Banana: Yellow Grapes: Black Apple: Red Banana: Green Grapes: Green Grapes: Brown Apple: Pale Red Banana: Greenish yellow Grapes: Brown Apple: Yellowish... (14 Replies)
Discussion started by: m6248m
14 Replies

5. Shell Programming and Scripting

Command Separator

It shows error at 'else', how can I fix it? if ; then # Note the space after the semicolon. #+ ^^ echo "File $filename exists."; cp $filename $filename.bak else # ^^ echo "File $filename not found."; touch $filename fi; echo "File test complete." (3 Replies)
Discussion started by: Qazi
3 Replies

6. Shell Programming and Scripting

Field separator X'1F'

Hi, I have a flat file with fields separated by a X'1F' i have to fetch 4th field from second line. please help me how to achieve it. I tried with below command and its not working. cut -f4 -d`echo -e '\x1f'` filename.txt I am using SunOS. Thanks in advance. (2 Replies)
Discussion started by: rohan10k
2 Replies

7. Shell Programming and Scripting

Using > as record separator

I have tried to use ">" as record separator, but it doesn't work. I have tried this: awk BEGIN{RS=">"}'{print $0}' input output: awk: BEGIN{RS=>}{print $0} awk: ^ syntax error awk BEGIN{RS="\>"}'{print $0}' input awk: BEGIN{RS=\>}{print $0} awk: ^ backslash not... (2 Replies)
Discussion started by: locoroco
2 Replies

8. Shell Programming and Scripting

field separator in Perl

is there a similar parameter you can set in perl like FS in awk? I think I've read all the tutorials on the subject, but cannot get this map split and so on thing to work. I need to sort a file by columns, eg. first, third, fifth... The script I need to add this column sorting is this: use... (38 Replies)
Discussion started by: ahsog
38 Replies

9. UNIX for Dummies Questions & Answers

Help with unix separator

can some one give me a list of unix separtor(s) if one than just the separator please thank you. (2 Replies)
Discussion started by: Black mage2021
2 Replies

10. Shell Programming and Scripting

Separator in Makefile?

all: $(LIBRARY) $(EXE) $(MAKEMAKE): @rm -f $(MAKEMAKE) $(PURIFY) $(CXX) -M $(INCLUDE) $(CPPFLAGS) *.cpp > $(MAKEMAKE) $(EXE): $(OBJS) $(LIBRARY) @echo "Creating a executable " $(PURIFY) $(CC) -o $(EXE) $(OBJS) $(ALLLDFLAGS) $(LIBS) This is a snippet... (2 Replies)
Discussion started by: laila63
2 Replies
Login or Register to Ask a Question