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
Solved

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

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

837 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