Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using $1, $2 in sed command in shell function

Posted on 2004-09-15
10
Medium Priority
?
422 Views
Last Modified: 2010-04-21
Hi Im trying to right a shell function that takes as its first arg a string/pattern to search for, and a second arg as the file or files in which to search.

testfind ()
{
echo "first argument is:  $1"
echo " second argument is:  $2"
sed -en "/$1/p" $2
}

[root@jekyll bin]# testfind he /root/parrot
first arg: he
second arg: /root/parrot
bash: the: command not found
[root@jekyll bin]#

also once I have created a function at the prompt how do I edit it?

TIA

0
Comment
Question by:BobNZ2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12072136
Hi BobNZ2,

What sed are you using .... the command sed -n "/$1/p" $2 works fine for me.

The error message is also is surprise since argument was he and not the ... was it a typo here?

Also make sure that in the original script there is a / before $1

Sunnycoder
0
 

Author Comment

by:BobNZ2
ID: 12072439
thanks for the reply

sed version is 4.0.5-1

"he" was what I was looking for; I have some test files with words containg he :)

yes there is a / before $1

is there a way of editing functions that are created on the cl or do you just overwrite?

B
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12072456
you can open it in a text editor like vim or emacs and edit it ....

Can you post the complete script ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Accepted Solution

by:
ahoffmann earned 750 total points
ID: 12072783
> sed -en ..
is wrong usage, must be
   sed -ne ..

> sed -en "/$1/p" $2
>  testfind he /root/parrot
here your pattern breaks sed syntax 'cause it contains / which is sed's delimiter
you either have to escape the / by \/ or use another sed command like s where you can use another delimiter
  sed -en "s:$1:&:p" $2
0
 

Author Comment

by:BobNZ2
ID: 12072795
that was it I was declaring and calling it from the commandline...

testfind ()
{
echo "first argument is:  $1"
echo " second argument is:  $2"
sed -en "/$1/p" $2
}

[root@jekyll bin]# testfind he /root/parrot
first arg: he
second arg: /root/parrot
bash: the: command not found
[root@jekyll bin]#

B
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12072824
BTW, the posted result does not match your posted script
    echo "first argument is:  $1"
vs.
    first arg: he
0
 

Author Comment

by:BobNZ2
ID: 12072848
ahoffman I thought that  sed used  /pattern/

do you mean escape the $ like: sed -ne "/\$1/p" \$2

B
0
 

Author Comment

by:BobNZ2
ID: 12072864
the first arg is he...

[root@jekyll bin]# testfind [B]he[/B] /root/parrot
first arg: he
second arg: /root/parrot
bash: the: command not found
[root@jekyll bin]#
0
 

Author Comment

by:BobNZ2
ID: 12072875
Hmmm cant use tags.
read [B]he[/B] as he

B
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12073399
> do you mean escape the $ like: sed -ne "/\$1/p" \$2
no
   sed -ne "/$1/p" $2
   testfind '\[B\]he\[\/B\]' /root/parrot

Keep in mind that the \ needs to pass your shell first, then it have to arive at sed. Depends on your shell how it reads and parses \ on commandline.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question