Re: get the first word of a file




"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


.



Relevant Pages

  • Re: Hamilton language?
    ... bashing their head against a brick wall. ... brick in the wall I can't be arsed bashing my head against... ... I'm "Dave" to my friends. ... The 'presumption' of 780 pages being useless is the most obvious case ...
    (rec.autos.sport.f1)
  • Re: Hamilton language?
    ... bashing their head against a brick wall. ... brick in the wall I can't be arsed bashing my head against... ... I'm "Dave" to my friends. ... The 'presumption' of 780 pages being useless is the most obvious case ...
    (rec.autos.sport.f1)
  • Re: Hamilton language?
    ... brick in the wall I can't be arsed bashing my head against... ... I'm "Dave" to my friends. ... Briatore that I quickly concluded I was wasting my time. ... The 'presumption' of 780 pages being useless is the most obvious case ...
    (rec.autos.sport.f1)
  • Re: Hard Core Rider
    ... thrown off, like at 60mph, would, really, ''bump'' ones head, and cause ... My main point above,is I think a rider falling off, might just have a ... better chance, than being thrown off, especially at high speeds. ... Above you state that a rider that comes off a motorcycle at 100 mph, ...
    (rec.motorcycles)
  • Re: Hard Core Rider
    ... thrown off, like at 60mph, would, really, ''bump'' ones head, and cause ... My main point above,is I think a rider falling off, might just have a ... better chance, than being thrown off, especially at high speeds. ... Above you state that a rider that comes off a motorcycle at 100 mph, ...
    (rec.motorcycles)