LinuxQuestions.org
Help answer threads with 0 replies.
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 07-29-2003, 01:22 PM   #1
jjfate
LQ Newbie
 
Registered: Jun 2003
Posts: 18

Rep: Reputation: 0
passing variables to sed


Is there a way to pass variables to sed? I would like to present a user with a list with line numbers, then prompt the user for the line number that they would like to delete. I thought that I would like to use sed to delete that line, here is what I had in mind:


cat list | more

echo "Enter line number to be deleted..."
read myNum
sed -e $myNumd


I know that I can do this to delete line 5

sed -e 5d

but I am having trouble passing variables. Any ideas? Or other solutions?

JJ
 
Old 07-29-2003, 06:44 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Something like
Code:
#!/bin/bash                                                                     
cat blah.txt | more

echo "Enter line number to be deleted..."
read myNum
cat blah.txt | sed -e "$myNum"d > blah.txt
cat blah.txt
?

Cheers,
Tink
 
Old 07-30-2003, 02:34 PM   #3
jjfate
LQ Newbie
 
Registered: Jun 2003
Posts: 18

Original Poster
Rep: Reputation: 0
Tinkster,

Perfect, thank you very much it worked great!


JJ
 
Old 07-30-2003, 03:44 PM   #4
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Code:
[ddipaolo@quinn ~]% echo -e "1\n2\n3\n4\n" > temp.txt
[ddipaolo@quinn ~]% cat temp.txt
1
2
3
4

[ddipaolo@quinn ~]% export LINENUM=3
[ddipaolo@quinn ~]% sed -e `echo $LINENUM`d temp.txt
1
2
4

[ddipaolo@quinn ~]%
You get -1 point for gratuitous cat'ing to sed Also, I'm not so sure those quotes will work for everything in general, but if it works for you great. If not, above is what I did.
 
Old 07-30-2003, 04:08 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
But your solution doesn't write back to
the same file ;)

Cheers,
Tink
 
Old 07-30-2003, 11:51 PM   #6
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
So redirect it to the file ...
 
Old 07-31-2003, 12:36 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Well done :)

Results in an empty file ...

Cheers,
Tink
 
Old 07-31-2003, 02:56 AM   #8
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Quote:
Originally posted by Tinkster
Well done

Results in an empty file ...

Cheers,
Tink
What?

Code:
cat temp.txt | sed -e `echo $LINENUM`d > temp.txt
If you want it to put the results in the same file, of course you'll have to use cat. But for things like piping the output of cat to more, it's just unnecessary. I was just pointing out that sed can work on files directly if you want it to.
 
Old 07-31-2003, 05:15 AM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
You get -1 point for gratuitous cat'ing to sed
:P

Cheers,
Tink
 
  


Reply


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
Passing variables to an include? Red Squirrel Linux - Software 4 09-23-2005 10:18 PM
Passing SSI variables between pages dubya Programming 2 07-05-2005 03:06 PM
Sed Command & Passing Values joey52 Linux - Newbie 4 12-27-2004 08:46 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
Passing variables in PHP???? vous Programming 3 03-20-2003 07:03 AM


All times are GMT -5. The time now is 05:31 AM.

Main Menu
Advertisement
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