OT: sed problem

Chris Pressey cpressey at catseye.mine.nu
Sat Jan 31 17:26:47 PST 2004


On Sun, 1 Feb 2004 01:38:44 +0000
Daniela <dgw at liwest.at> wrote:

> I was wondering how I can do the following with sed (or another
> program): 1. Output only the text from the start of the line to the
> first pipe character 2. Output only the text between the last and the
> previous pipe character Or, split the line at the pipe characters and
> assign the parts to different shell variables.

It sounds like awk might be better suited to what you want to do than
sed.

You should be able to do something like this with sh and awk:

	cat test.txt | awk -F'|' '{ print $1,$2,$3 }' | \
	while read VAR1 VAR2 VAR3; do
 	       # do something with $VAR1, $VAR2, and $VAR3
	done

So if test.txt contains

	a|b|c
	d|e|f

On the first iteration of the while loop, VAR1=a, VAR2=b, and VAR3=c,
and on the second iteration... well, you get the idea :)

-Chris


More information about the freebsd-questions mailing list