sed command in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command in perl script
# 1  
Old 02-17-2010
sed command in perl script

What is wrong with this line in a perl script?

Code:
 
$amc_data = `sed -n '/\[Amc\]/,/\[\/Amc\]/p' "$config_file"`

I ran the above from command line and it works fine from unix command prompt.
The code should produce output between the [Amc] and [/Amc] tags.

The config_file is as follows:

Code:
 
[Amc]
Sun     0000-2359
Mon     0000-0859;1830-2359
Tue     0000-2359;1830-2359
Wed     0000-2359;1830-2359
Thu     0000-2359;1830-2359
Fri     0000-2359;1830-2359
Sat     0000-2359
[/Amc]
[Dummy]
Sun     0000-2359
Mon     0000-0859;1830-2359
Tue     0000-2359;1830-2359
Wed     0000-2359;1830-2359
Thu     0000-2359;1830-2359
Fri     0000-2359;1830-2359
Sat     0000-2359
[/Dummy]

# 2  
Old 02-17-2010
Hello,

First of all read it in list context. It would be easier to parse it afterwards.

I would suggest you use a perl one liner only . Incorporate this logic in your script or use a sed module. using a shell command would probably make it slower.

Code:
gaurav@localhost:~$ perl -wln -e 'BEGIN{my $flag=0;my @amc_data=();}$flag=1 if /\[Amc\]/;push(@amc_data,$_) if $flag==1;$flag=0 if /\[\/Amc\]/;END{print @amc_data;}' amc

So your code would look like
Code:
my $flag=0;
my $arr=();
$flag=1 if /\[Amc\]/;
push(@amc_data,$_) if $flag==1;
$flag=0 if /\[\/Amc\]/;

This is exactly what sed does.

Regards,
Gaurav.
# 3  
Old 02-18-2010
Hi,
Thanks for your reply but I did not understand how the script will work.
Can you please explain?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl or sed command ?

Hi Guys Am working on a bash script but got stuck, in this line: 32 $configValues = ''; What would be the best command to enter the password between the " Perl or sed ? Been trying with Perl using this command: perl -pi -e 's/''/Seattle#1669!/g'... (5 Replies)
Discussion started by: Tox
5 Replies

2. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

3. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

4. Shell Programming and Scripting

Command to remove duplicate lines with perl,sed,awk

Input: hello hello hello hello monkey donkey hello hello drink dance drink Output should be: hello hello monkey donkey drink dance (9 Replies)
Discussion started by: cola
9 Replies

5. Shell Programming and Scripting

Perl & Sed command -- Out of Memory Error

Experts, We used to receive our source files with '~^' as row delimiter. This file contains 2500K records and two of the columns having value in HTML formats within the file. While running the below commands against the file, we are encountering out of memory, could you please help to... (3 Replies)
Discussion started by: srivijay81
3 Replies

6. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies

7. Shell Programming and Scripting

Convert Sed command to perl command

Hello, Can any perl experts help me convert my sed string to perl. I am unsuccessful with this. I have to remove this string from html files OAS_AD('Top'); I have come up with this. However the requirement is in perl. for find in $(find . -type f -name "file1.html") ; do cat $find |... (2 Replies)
Discussion started by: abacus
2 Replies

8. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

9. UNIX for Dummies Questions & Answers

how to use sed or perl command to find and replace a directory in a file

how to use sed command to find and replace a directory i have a file.. which contains lot of paths ... for eg.. file contains.. /usr/kk/rr/12345/1 /usr/kk/rr/12345/2 /usr/kk/rr/12345/3 /usr/kk/rr/12345/4 /usr/kk/rr/12345/5 /usr/kk/rr/12345/6 /usr/kk/rr/12345/7... (1 Reply)
Discussion started by: wip_vasikaran
1 Replies

10. Shell Programming and Scripting

Perl/Sed script help

Hi All, I would need to generate Oracle Inster scripts from an excel formatted spreadsheet as follows: This needs to be stripped as follows: REC 1, REC 2 etc are the separators of the records ... I beleive a pearl script can be written for this ... anything useful will be of... (8 Replies)
Discussion started by: sabyasm
8 Replies
Login or Register to Ask a Question