Re: get the first word of a file
- From: "Dan Mercer" <dmercer@xxxxxxxxx>
- Date: Fri, 23 Dec 2005 23:44:25 GMT
"Stephane CHAZELAS" <this.address@xxxxxxxxxx> wrote in message news:slrndqni3s.5fh.stephane.chazelas@xxxxxxxxxxxxxxxxxx
: 2005-12-22, 23:49(+00), Dan Mercer:
: >
: > "Hubble" <reiner@xxxxxxxxx> wrote in message news:1135253026.093269.8430@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
: > : >to obtain the FIRST DATE ONLY
: > : >cat data | sed s/\|.*// | head -1
: > :
: > : I woud change the order of sed and head, since sed handles here every
: > : line which is then thrown away by head. Why not
: > :
: > : cat data | head -1 | sed s/\|.*//
: > :
: > : This should require less cpu
: >
: > Use:
: > IFS='|' read data waste < file
: >
: > Instead of a useless use of cat and a useless use of sed.
: [...]
:
: That's an incorrect use of read.
:
: IFS='|' read -r data waste < file
:
: unless you want "\" to act as an escape character for the
: separator.
20051112|data|moreData|evenMoreData|..|
20051112|data|moreData|evenMoreData|..|
20051112|data|moreData|evenMoreData|..|
20051112|data|moreData|evenMoreData|..|
20051112|data|moreData|evenMoreData|..|
20051112|data|moreData|evenMoreData|..|
20051112|data|moreData|evenMoreData|..|
Do you see a "\"? I try not to confuse people
by overloading them with extraneous details.
:
: And to have the same behavior as sed, you need to add:
:
: IFS='|' read -r data waste < file &&
: printf '%s\n' "$data"
:
: I don't know why you would prefer that to:
:
: sed 's/|.*//;q' < file
A lot of reasons:
o - you don't always have sed. The sed on my Cygwin
started going haywire after I reinstalled recently. Some
wierd interaction between Cygwin, Firefox and M$ (It only
happens when Firefox is up - go figure). It blows up in USER.EXE
and I usually have to reboot. What can I say, ME blows.
When I got a Linspire box from WalMart for <$300, it came
without sed altogether. It came with the barest of tools and I
had to piece it together by hand. The first thing I installed
was ksh93 which gave me reasonable abilities.
I had an inept sysadm trash a box by screwing up a tarball
express, clobbering /usr/bin in the process. I was able to kill
it and, because ksh was running, it survived. Because I
had a good tool and knew how to use it I was able to salvage
the system without having to rebuild, which saved us days
and saved a full days work for our programmers (at a $1000
a programmer day it amounted to a $12000 savings). Fortunately
for the consulting company I worked for the SA was the
customers and not ours.
o - I first learned UNIX on Amdahl UTS running on an overburdened
CMS mainframe, then graduated to a Tower 600 (68000 chip,
- IIRC same as a commodore 64 - no cache memory). Running
sed, heck, just laoding it, wasn't fast
o - I come from the Grace Hopper school of programming - cycles matter
My method is the fastest, cleanest and fits the data. it is a
technique any shell programmer should know.
Heck, if you're going to mess with sed (which produces absolutely
impenetrable code) you might as well learn a real language with some
teeth like perl.
Dan Mercer
:
: though.
:
: (not that behaviors may differ if file is not a text file).
:
: --
: Stéphane
.
- References:
- get the first word of a file
- From: dufffman
- Re: get the first word of a file
- From: William.Deans@xxxxxxxxx
- Re: get the first word of a file
- From: Hubble
- Re: get the first word of a file
- From: Dan Mercer
- Re: get the first word of a file
- From: Stephane CHAZELAS
- get the first word of a file
- Prev by Date: Re: Split a string
- Next by Date: Re: get the first word of a file
- Previous by thread: Re: get the first word of a file
- Next by thread: Re: get the first word of a file
- Index(es):
Relevant Pages
|