Date: Sun, 1 Feb 2004 19:52:42 +0000 From: Daniela <dgw@liwest.at> To: Chris Pressey <cpressey@catseye.mine.nu> Cc: questions@freebsd.org Subject: Re: OT: sed problem Message-ID: <200402011952.42130.dgw@liwest.at> In-Reply-To: <20040201101027.2b90499d.cpressey@catseye.mine.nu> References: <200402010138.44102.dgw@liwest.at> <200402011533.58877.dgw@liwest.at> <20040201101027.2b90499d.cpressey@catseye.mine.nu>
next in thread | previous in thread | raw e-mail | index | archive | help
On Sunday 01 February 2004 18:10, Chris Pressey wrote: > On Sun, 1 Feb 2004 15:33:58 +0000 > > Daniela <dgw@liwest.at> wrote: > > On Sunday 01 February 2004 01:34, Jez Hancock wrote: > > > On Sun, Feb 01, 2004 at 01:38:44AM +0000, Daniela 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. > > > > > > #!/bin/sh > > > test="one|two|three" > > > set `echo "$test" | sed -e 's/\|/ /g'` > > > > > > # $1="one", $2="two", $3="three": > > > echo $@ > > > > This doesn't work when the parts between the pipe characters contain > > spaces themselves. > > Nor does the awk approach I posted, incidentally - the problem being > that sh likes to split up its input into variables, by spaces. awk -F'|' '{ print $1 }' does work indeed. This way, I can assign the parts to different variables, and dismiss the parts I don't need. > You can work around it by translating the spaces into some unused > character first, then translating pipes to spaces, then finally > translating the unused characters back into spaces in each of the > variables. > > There may be a simpler way, but if so, I don't know what it is. > > Or you could avoid sh variables and do whatever processing you have to > do entirely in awk (or perl.) I really like csh programming. Everyone says that csh is crap for scripting, but it isn't. I think Perl is harder than csh. But let's not start another holy war.
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200402011952.42130.dgw>