NoelC
NoelC

Reputation: 1419

How to use sed to isolate only the first part of a file

I'm running Windows and have the GnuWin32 toolkit, which includes sed. Specifically:

C:\TEMP>sed --version

GNU sed version 4.2.1

I have a text file with two sections: A fixed part I want to preserve, and a part that's appended after running a job.

In the file is a unique string that identifies the start of the part that's added, and I'd like to use Gnu sed to isolate only the part of the file that's before the unique string - i.e., so I can append different data to the fixed part each time the job is run.

I know I could keep the fixed portion in a separate file, but that adds complexity and it would be more elegant if I could just reuse the data at the start of the same file.

A long time ago I knew how to set up sed scripts, and I'm sure this can be done with sed, but I've slept since then. :)

Can you please describe how to use sed to display the lines of text in a file up to and not including a specific string?

Example:

line 1 of fixed portion
line 2 of fixed portion
unique string
line 1 of appended portion
line 2 of appended portion
line 3 of appended portion

What I'd like is to see as output:

line 1 of fixed portion
line 2 of fixed portion

I've gotten as far as:

sed -r -n -e "0,/unique string/p"

but that prints the unique string as well.

Thanks in advance.

-Noel

Upvotes: 1

Views: 228

Answers (1)

hek2mgl
hek2mgl

Reputation: 158130

This should work for you:

sed -n '/unique string/q;p' file

It quits processing at unique string. Other lines get printed.

An alternative might be to use a range address like this:

sed -n '1,/unique string/{/unique string/!p}' file

Note that sed includes the range border. We need to exclude unique string from printing.

Furthermore I'm using the -n option which makes sed suppress the output of input lines by default.


One thing, if unique string can contain characters which are also syntax characters in the regex like ...

test*

... sed might not be the right tool for the job any more since it can only match regular expressions but not fixed strings.

In that case awk might be the tool of choice:

awk 'index("*unique string*"){exit}1' file

index("string") returns a non zero value (the position) if the string has been found. We cancel further processing of input lines in that case and don't print that line as well.

The trailing 1 always evaluates to true and makes awk print all the lines until the previous condition applies.

Upvotes: 2

Related Questions