help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] first part


From: Eduardo Bustamante
Subject: Re: [Help-bash] first part
Date: Sat, 21 May 2016 08:56:53 -0500

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
>>
>>



reply via email to

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