Solved

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

Posted on 2004-09-15
10
382 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now