[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] first part

From: Richard Lohman
Subject: Re: [Help-bash] first part
Date: Sat, 21 May 2016 12:49:03 -0500

A couple of points

- Your example solves the same problem; it would seem that there is more
than one way to solve problems in Linux. Who knew?

- My take on providing examples is focus precisely on the atomic question
asked, not to assume the problem space (is the OP using a file containing
nothing but data formatted as shown? I don't know). I saw the original post
as how to extract a substring from text with '-' as the field separator. I
really think awk is a good use case for that problem. Would it be better to
process the entire file with awk in one shot rather than a line at a time?
Absolutely, but again, that was not the original question. I would leave it
to the OP to work out feeding awk and iteration.

- The echo at the beginning of the line was not intended as part of the
solution; rather an example (which may or may not be the best) to feed awk.
The answer was awk.

At first I was a bit offended by your opening statement. But then I looked
through some past conversations on the listserv only to find that I am not
the only person with whom you have trouble being polite. Most unfortunate,
given your obvious command of the shell.

@Val: Ed is correct, there are much better ways to feed data into awk than

Best regards,

On Sat, May 21, 2016 at 8:56 AM, Eduardo Bustamante <address@hidden>

> I'm sorry, but this is actually bad advice.
> * There's no point in forking awk for every line, you can just run awk
> on the whole file
> * You are not quoting $field correctly (it's "$field", not $field)
> * 'echo $var' might fail for cases where $var looks like a flag
> Use:
> while IFS=- read -r f rest; do
>   printf '%s\n' "$f"
> done < file
> No forking, no quoting problem, no issues with funny input.
> On Sat, May 21, 2016 at 8:10 AM, Richard Lohman <address@hidden>
> wrote:
> > Pipe the output of the field into awk:
> >
> > echo $field | awk -F '-' '{print $1}'
> > On May 21, 2016 7:45 AM, "Val Krem" <address@hidden> wrote:
> >
> >>
> >>
> >> Hi all,
> >> I  have field  that look like the following
> >> 124-20-25
> >> 014-2012-26
> >> 1024-212-27
> >> 1-001-29
> >>
> >> I want extract the first part of the string,  prior to the first "-"
> >> 124
> >> 014
> >> 1024
> >>
> >> 1
> >>
> >> How do I do I that?
> >> Thank you in advance
> >>
> >>

Richard J. Lohman

reply via email to

[Prev in Thread] Current Thread [Next in Thread]