Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to import an excerpt from a file into a script for processing.

LIST=$(sed '1,/+++NETWORKLIST+++/d' < /path/to/file | sort -t ";" -k2,2r)

My problem is that this script is not deleting the material I don't want to pass on - I want to delete all the lines up to and including NETWORKLIST. (Beyond that point is entirely a list, with fields separted by semicolons, which I want to pass on to be sorted.)

How do I pass on just an experpt from a file?

share|improve this question
how is the file arranged? – GnP Feb 10 '14 at 20:25
what do you mean? – operalala Feb 10 '14 at 20:27
@operalala Hi. Are you trying to "process" the section of text or are you trying to delete it? Please provide more information about the file contents. – user13742 Feb 10 '14 at 20:34
@operalala. We need more info about the file. Are there two sections, and by deleting everything up to "NETWORKLIST" you will end up with just the section you need? If possible, delete any sensible information from the file and upload it to your question. – GnP Feb 10 '14 at 20:41
I tried to reproduce with the information you provided and it works for me. I get in the LIST variable only the stuff after +++NETWORKLIST+++ – Ketan Feb 10 '14 at 21:31

Let's say you have the following file:

line with spaces, just in case
and with    tabs


And you want to extract only the NETWORKLIST section like this:


Then your sed mojo to do it is:

sed -n '/+++NETWORKLIST+++/,/^+/ { x; /^$/! p; }' testfile

Disection of a sed command

This is an address range pattern:


It will match all the lines starting at +++NETWORKLIST+++ and ending at a line starting with +.

Then comes the commands:

The { opens a command list (it's not usually seen as most users of sed actually use a single command after matching).

The x; exchanges the contents of the hold space and the pattern space (see The Concept of 'Hold space' and 'Pattern space' in sed).

Then, we want to print nonempty lines: /^$/! p;

Also, close command list: }

Why do this exchange? Well, after matching the range, we're left with this input:



But we are not interested in the section header for the next section.

Thus, we tell sed to put each line on the hold space.

If the file had a section closer such as ---NETWORKLIST--- we would simply do:

sed -n '/+++NETWORKLIST+++/,/^-/ p'

Or even this if we don't want the section finalizer:

sed -n '/+++NETWORKLIST+++/,/^-/ {/^-/d; p}'

But if we try that for OP's input, we would loose the section header from the relevant section.

By doing the x on each step, we are effectively skipping the last line of the range:

  1. PatternSpace=+++NETWORKLIST+++, HoldSpace=^$. Exchange them
  2. PatternSpace=thisisrelevant, HoldSpace=+++NETWORKLIST+++. Exchange them, print PatternSpace
  3. PatternSpace=thistoo, HoldSpace=thisisrelevant. Exchange them, print PatternSpace
  4. PatternSpace=^$, HoldSpace=thistoo. Exchange them, print PatternSpace
  5. PatternSpace=+++ANOTHERSECTION+++, HoldSpace=^$. Exchange them.
  6. There's no more input

Please leave a comment if it's unclear or I used the wrong name for a sed concept.

share|improve this answer
could you please explain your sed line. – user55518 Feb 11 '14 at 19:14
@bersch: there you go – GnP Feb 11 '14 at 20:03
The problem does not appear to be the sed command, but something with the file that is not working, or is choking sed, or something. The file is 150 lines, and I don't know how to post it. – operalala Feb 12 '14 at 13:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.