LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-15-2011, 07:44 PM   #1
odstderek
LQ Newbie
 
Registered: Nov 2011
Location: USA
Distribution: Backtrack
Posts: 9

Rep: Reputation: Disabled
Exclamation sed range


Hello!

I'm working on a script in bash, and I have 2 parameters from the user, $4 and $5. I want to delete this range of lines. This is what I have ($1 is the file we're deleting from):

Code:
touch temp
sed "$4,$5 d" > temp
mv temp $1
In theory this should create a temporary file, delete the line range and put it into the temporary file, and then replace $1 with this temporary file.

The 2nd line isn't working. When I do this with 2 normal numbers it works, but not with 2 variables containing numbers. Any ideas on how to fix this? What I end up getting is nothing written to the temporary file at all, so $1 becomes a blank file.

Although the code above doesn't work, deleting a single line like this works:
Code:
touch temp
sed "$4 d" > temp
mv temp $1
What am I missing here??
 
Old 11-15-2011, 10:36 PM   #2
davemguru
Member
 
Registered: Apr 2006
Location: London
Distribution: Pclos,Debian,Puppy,Fedora
Posts: 87

Rep: Reputation: 42
How about this.....

Quote:
Originally Posted by odstderek View Post
Hello!

I'm working on a script in bash, and I have 2 parameters from the user, $4 and $5. I want to delete this range of lines. This is what I have ($1 is the file we're deleting from):

Code:
touch temp
sed "$4,$5 d" > temp
mv temp $1
In theory this should create a temporary file, delete the line range and put it into the temporary file, and then replace $1 with this temporary file.

The 2nd line isn't working. When I do this with 2 normal numbers it works, but not with 2 variables containing numbers. Any ideas on how to fix this? What I end up getting is nothing written to the temporary file at all, so $1 becomes a blank file.

Although the code above doesn't work, deleting a single line like this works:
Code:
touch temp
sed "$4 d" > temp
mv temp $1
What am I missing here??
I am only working with what you have written.... but, where is it that you are using $1 (the input file)??
I would write
Code:
sed "$4,$5d" $1 > temp
mv temp $1
Which works for me. (No need to "touch" (create) the file temp in the first instance - as it will be created by the stdout argument to sed.
 
Old 11-16-2011, 12:01 AM   #3
odstderek
LQ Newbie
 
Registered: Nov 2011
Location: USA
Distribution: Backtrack
Posts: 9

Original Poster
Rep: Reputation: Disabled
well, mystery solved. simple mistake. thanks!
 
  


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
[SOLVED] sed print range from pattern1 to pattern2 porphyry5 Programming 7 06-15-2011 09:57 AM
[SOLVED] SED and Replacing Specific occurrence or Range of Lines bridrod Linux - Newbie 7 08-27-2009 09:59 AM
[SOLVED] sed print range to end of file schneidz Programming 4 04-08-2009 01:30 PM
Delete a line range with sed J_Szucs Programming 8 04-29-2008 11:43 PM
Sed - replace text only in a certain range twantrd Programming 4 11-30-2006 06:54 PM


All times are GMT -5. The time now is 12:02 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration