LinuxQuestions.org
Review your favorite Linux distribution.
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 03-22-2008, 01:38 PM   #1
Mangled
LQ Newbie
 
Registered: Jun 2007
Posts: 13

Rep: Reputation: 0
sed help


Hi all

As far as I'v seen sed depends on a line number or a constant in the file, ie changing line 1 or changing 123 to 321.
Is it possible to change a variable if you have another constant in a line as in:
changing 'this is somenumber' to 'this is anothernumber' if you don't know what the line or variable is.

Should I be using sed or is there something better/easer?
 
Old 03-22-2008, 02:38 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

You can use 'search ranges' instead of (or combining with) line numbers. I.e.:

sed -n '/foo/,/bar/p' infile will print all between the first line with foo (included) and the last line with bar (also included).

You can combine real linenumbers with searchstrings:

sed -n '2,/foobar/p' infile will print from line 2 to the last line containing foobar.

Sed also accepts variables (which could be set and reset in a script for example):

THREE="3"
sed -n "$THREE,4p" infile


The above will print lines 3 and 4. Do mind the double quotes instead of the single quotes.

Hope this helps.

Last edited by druuna; 03-22-2008 at 02:39 PM.
 
Old 03-22-2008, 03:18 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by Mangled View Post
Hi all

As far as I'v seen sed depends on a line number or a constant in the file, ie changing line 1 or changing 123 to 321.
Is it possible to change a variable if you have another constant in a line as in:
changing 'this is somenumber' to 'this is anothernumber' if you don't know what the line or variable is.

Should I be using sed or is there something better/easer?
You can USE a variable in SED (as shown by Druna). You cannot use SED to change a variable. SED operates on a stream of data (from a file or elsewhere) and makes changes on the fly. You can bring in the content of a variable (i.e. create a copy), but you can't change the original using SED.

Here is the best SED tutorial I have seen:
http://www.grymoire.com/Unix/Sed.html

I suggest you post an example (excerpt) of a file that you want to process, and the kind of change you want to make.
 
  


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
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
More SED help facets Programming 2 06-03-2004 12:02 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:32 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