LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 12: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, 05:44 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 01: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, 02: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, 03:08 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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

Cheers,
Tink
 
Old 07-30-2003, 10: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-30-2003, 11:36 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Well done :)

Results in an empty file ...

Cheers,
Tink
 
Old 07-31-2003, 01: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, 04:15 AM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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

Cheers,
Tink
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:10 PM.

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
Open Source Consulting | Domain Registration