Solved

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

Posted on 2004-09-15
10
388 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

12 Experts available now in Live!

Get 1:1 Help Now