LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-02-2011, 06:41 PM   #1
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,445

Rep: Reputation: 83
How to pass sed address by shell variable?


Hya,

Situation:
Code:
>cat tux
L
I 
N
U
X
>cat tux | sed -e '2s/./<&>/'
L
<I>
N
U
X
Goal: The address for sed s command, (this case 2), need to be supplied by shell variable, like the code below.
Incorrect shellscript
Code:
#!/usr/bin/tcsh
sed -i -e "{$1}s/./<&>/" tux
I think this should work, but not. Then, after several try-and-errors I found that
Code:
sed  -e $1's/./<&>/'
works, but it looks ugly.

I am somewhat confident that there is a neater way.

Question: Does anybody know the answer?

cheers

Last edited by kaz2100; 08-02-2011 at 07:39 PM. Reason: format
 
Old 08-02-2011, 07:13 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by kaz2100 View Post
Code:
sed  -e $1's/./<&>/'
works, but it looks ugly.

I am somewhat confident that there is a neater way.

Question: Does anybody know the answer?
With shell scripting the truth is often ugly. There isn't much room for improvement besides sed -e "$1"'s/./<&>/'. This would certainly complicate writing an actual #!/bin/sed script; I'm surprised referencing the environment isn't a standard part of sed (although $1 isn't inherited). I think your problems with "" stem from tcsh syntax because sed -i -e "{$1}s/./<&>/" tux is acceptable in bash.
Kevin Barry

Last edited by ta0kira; 08-02-2011 at 07:16 PM.
 
Old 08-02-2011, 08:01 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

try this:
Code:
#!/usr/bin/tcsh
sed -i -e "${1} s/./<&>/" tux
Notice, that the curly braces do NOT enclose the '$'.
 
1 members found this post helpful.
Old 08-02-2011, 08:21 PM   #4
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,445

Original Poster
Rep: Reputation: 83
Hya,

Thanks,

ta0kira: Yes, often shell scripts are ugly. As you mentioned, I do not want unnecessarily complicated scripts, nor error prone ones.

crts: Yes, you are a star!

cheers
 
  


Reply

Tags
address, sed, shell_variable, tcsh


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass a shell variable to an AWK command chogall Programming 1 12-23-2010 10:12 AM
Pass a variable as a line number in sed dx0r515t Programming 7 03-30-2007 04:55 PM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
Expanding shell variable in sed goofyheadedpunk Programming 4 09-11-2004 05:17 PM


All times are GMT -5. The time now is 08:13 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration