LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed help (https://www.linuxquestions.org/questions/linux-newbie-8/sed-help-629888/)

Mangled 03-22-2008 12:38 PM

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?

druuna 03-22-2008 01:38 PM

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.

pixellany 03-22-2008 02:18 PM

Quote:

Originally Posted by Mangled (Post 3097052)
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.


All times are GMT -5. The time now is 06:11 PM.