Solved

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

Posted on 2004-09-15
10
400 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 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

735 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