How to use parameter with perl function?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use parameter with perl function?
# 1  
Old 05-13-2010
How to use parameter with perl function?

am a beginer of shell Unix, plesase tell me how to get parameter with $perl in loop
Code:
$ perl -lne '$/="DOCEND";print $_."DOCEND" if /$ACC/' file_input > output

i can't get parameter in loop with perl fucntion like this


pass paramerter to $ACC not accept in this script

$ACC = paramerter to find in text file_input / Ex. $ACC = ("KRAI","MARK")

EX.file_input
Code:
DOCSTART
DFSDF
NAME FIRSTNAME
ADDRESS
DSAFFD
DOCEND
DOCSTART
VEDFDSF
NAME KRAI
ADDRESS
SDFDSG
DOCEND
DOCSTART
LOUI
NAME ROBERT
ADDRESS
GHHW
DOCEND
DOCSTART
YYUI
NAME MARK
ADDRESS
LKJL;
DOCEND
DOCSTART
SAFD
NAME BEN
ADDRESS
8OIP
DOCEND

I need out put from loop with $perl like below

output text file
Code:
DOCSTART
VEDFDSF
NAME KRAI
ADDRESS
SDFDSG
DOCEND
DOCSTART
LOUI
NAME MARK
ADDRESS
GHHW


Last edited by Scott; 05-13-2010 at 08:06 AM.. Reason: Removed formatting, added code tags
# 2  
Old 05-13-2010
Maybe like this ?

Code:
$
$
$ cat -n f4
     1  DOCSTART
     2  DFSDF
     3  NAME FIRSTNAME
     4  ADDRESS
     5  DSAFFD
     6  DOCEND
     7  DOCSTART
     8  VEDFDSF
     9  NAME KRAI
    10  ADDRESS
    11  SDFDSG
    12  DOCEND
    13  DOCSTART
    14  LOUI
    15  NAME ROBERT
    16  ADDRESS
    17  GHHW
    18  DOCEND
    19  DOCSTART
    20  YYUI
    21  NAME MARK
    22  ADDRESS
    23  LKJL;
    24  DOCEND
    25  DOCSTART
    26  SAFD
    27  NAME BEN
    28  ADDRESS
    29  8OIP
    30  DOCEND
$
$
$ perl -lne '$/="DOCEND"; print $_."DOCEND" if /MARK|KRAI/' f4
 
DOCSTART
VEDFDSF
NAME KRAI
ADDRESS
SDFDSG
DOCEND
 
DOCSTART
YYUI
NAME MARK
ADDRESS
LKJL;
DOCEND
$
$

Or this -

Code:
$
$ perl -lne 'BEGIN {$/="DOCEND"; $ACC="MARK|KRAI"} print $_."DOCEND" if /$ACC/' f4
 
DOCSTART
VEDFDSF
NAME KRAI
ADDRESS
SDFDSG
DOCEND
 
DOCSTART
YYUI
NAME MARK
ADDRESS
LKJL;
DOCEND
$
$

tyler_durden
# 3  
Old 05-13-2010
Power

am try to cut from your script but not work in my shell loop T_T

Code:
while read I
       do
         ACCT=`echo ${I} |awk -F'|' '{print $1}'`
         GMF=`echo ${I} |awk -F'|' '{print $2}'`
         perl -lne 'BEGIN {$/="DOCEND"; $ACCT} print $_."DOCEND" if /$ACCT/'  ${GMF}  > output 
done < listfile.txt

listfile.txt
Code:
 
 KRAI|file_name 
 MARK|file_name

file_name = file_input

bcoz in my loop want to cut text file from DOCSTART to DOCEND of listfile.txt , and i used loop bcoz it had many ACCT(name of customer) in many file

by the way : script in one line can cut text or data (if i put the name to find direct in script)

but same script not work when i use in my shell loop SmilieSmilie

please help me !!!!Smilie

sorry !! am weak in englishSmilie

Last edited by Scott; 05-13-2010 at 10:55 AM.. Reason: Code tags, please...
# 4  
Old 05-13-2010
In order to pass shell variables to a Perl one-liner, use double quotes instead of single quotes.

Have a look at the following testcase that I set up to mimic your scenario:

Code:
$
$ # list the contents of data files - "file_input1" and "file_input2"
$ cat file_input1
DOCSTART
DFSDF
NAME ADAM
ADDRESS
DSAFFD
DOCEND
DOCSTART
VEDFDSF
NAME KRAI
ADDRESS
SDFDSG
DOCEND
DOCSTART
YYUI
NAME MARK
ADDRESS
LKJL;
DOCEND
DOCSTART
SAFD
NAME BEN
ADDRESS
8OIP
DOCEND
$
$ cat file_input2
DOCSTART
DFSDF
NAME ADAM
ADDRESS
DSAFFD
DOCEND
DOCSTART
VEDFDSF
NAME KRAI
ADDRESS
SDFDSG
DOCEND
DOCSTART
YYUI
NAME MARK
ADDRESS
LKJL;
DOCEND
DOCSTART
LOUI
NAME ROBERT
ADDRESS
GHHW
DOCEND
$
$ # show what's in the list file
$ cat listfile.txt
KRAI|file_input1
MARK|file_input2
$
$ # display the content of the Bash shell script
$ cat getnameblock.sh
#!/usr/bin/bash
while read I
do
  ACCT=`echo ${I} |awk -F'|' '{print $1}'`
  GMF=`echo ${I} |awk -F'|' '{print $2}'`
  perl -lne "BEGIN {\$/=\"DOCEND\"} print \"\$_\",\"DOCEND\" if /$ACCT/"  ${GMF}
done < listfile.txt
$
$ # now execute the shell script
$ ./getnameblock.sh
 
DOCSTART
VEDFDSF
NAME KRAI
ADDRESS
SDFDSG
DOCEND
 
DOCSTART
YYUI
NAME MARK
ADDRESS
LKJL;
DOCEND
$
$

HTH,
tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 05-14-2010
Smiliethank a lot durden_tyler for script and knowledgeSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass function parameter to do loop?

Hi All, I have created one function for KSH and was running well with one parameter input since I just had to use $1 to get the parameter. Now I want to do loop for each parameter(actually filenames) . I have try to use do loop, but $i does not resolve to parameter instead it resolves to 1,... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

Passing command as a function parameter

Hi All, Just trying to implement the below shell script using AIX ksh shell. myfunc { eval "$*" } CMD='ls -la /etc/hosts | awk '{print $9"|"$5}'' myfunc $CMD Keeping getting "|}: not found" errors, any pointers would greatly be appreciated. Kind Regards Ed Please... (2 Replies)
Discussion started by: eo29
2 Replies

3. Shell Programming and Scripting

How to get the parameter value from the parameter file in perl?

hi all, i have a parameter file of following format, i want a method which can get the value of specific parameter. parameter file format: <Parameter Name="FileLocationWindows"> <Description> The directory location of the logger file. ... (1 Reply)
Discussion started by: laxmikant.hcl
1 Replies

4. Shell Programming and Scripting

Pass command as a function parameter

Hi guys, can someome help with this question, I have defined a function that takes a command as a parameter, but when the command is executed from the function it will throw errors because what I believe is a special character escaping issue. I tried using the backslash to escape the pipe | and >... (2 Replies)
Discussion started by: marouanix
2 Replies

5. UNIX for Dummies Questions & Answers

function parameter issue

hi, i have a function say "mask". i'm passing parameters to the function like file_name \$1 |. inside the function i'm naming the second parameter as position (i.e value of position will be "$1"). i'm passing the position's value like " $position=a" But it is nor picking the value "$1".... (5 Replies)
Discussion started by: amar1003
5 Replies

6. Solaris

function parameter in unix

Hi, How to use a function with passing value as a parameter in unix ? With Regards (7 Replies)
Discussion started by: milink
7 Replies

7. Shell Programming and Scripting

pass parameter to function

HI all I have a code like ############################################## minyear() { curryear=$1 echo $curryear } ##Main Program ## minyear exit ####### when i execute "sh scriptname 2005" output should be like 2005 but the output is blank. I guess i need to pass parameter to... (3 Replies)
Discussion started by: vasuarjula
3 Replies

8. Shell Programming and Scripting

KSH list as function parameter

Hello, Could anyone help me with some KSH syntax? I'm trying to pass a list as a function parameter in a KSH? For example I have code like this: print_counter() { N=$1 C=$2 for A in $C; do echo "This is $N number $A" done } NAME=BRICK COUNT=" 1 2 3 4" ... (2 Replies)
Discussion started by: pn8830
2 Replies

9. Shell Programming and Scripting

function parameter

Hi, i have this code : awk -v s=string_to_find 'BEGIN{ n="" } { if ($0 ~ /FEATURE TESTED /) { n=$0 } if (index($0,s)!=0) { if (n!="") print n n=="" } }' $file1 > $file2 (4 Replies)
Discussion started by: kamel.seg
4 Replies

10. UNIX for Advanced & Expert Users

Parameter passing in a function

I need to pass a parameter to a function in a script. My parameter is a string. When I display the parameter within my function, I only get the first word from string I pass in. How can I make the function receive the whole string (and not terminate at the first space it encounters)?. part of... (2 Replies)
Discussion started by: fastgoon
2 Replies
Login or Register to Ask a Question