Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can I combine two sub() commands into one?

22 views
Skip to first unread message

Hongyi Zhao

unread,
Oct 13, 2016, 4:34:23 AM10/13/16
to
Hi all,

I use the following two commands in my awk script:

...
sub(/^.*\//, "", FILENAME)
sub(/_.*$/, "", FILENAME)
...

Can I combine these two sub() commands into one?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Janis Papanagnou

unread,
Oct 13, 2016, 4:36:23 AM10/13/16
to
On 13.10.2016 10:34, Hongyi Zhao wrote:
> Hi all,
>
> I use the following two commands in my awk script:
>
> ...
> sub(/^.*\//, "", FILENAME)
> sub(/_.*$/, "", FILENAME)
> ...
>
> Can I combine these two sub() commands into one?

Yes, the pipe symbol '|' is the logical OR operator.
(It's documented.)

Janis

>
> Regards
>

Hongyi Zhao

unread,
Oct 13, 2016, 7:13:58 AM10/13/16
to
On Thu, 13 Oct 2016 10:36:22 +0200, Janis Papanagnou wrote:

>> Can I combine these two sub() commands into one?
>
> Yes, the pipe symbol '|' is the logical OR operator.
> (It's documented.)

Seems it doesn't work for me:

$ echo /bin/tun987_vpngate_14.38.19.182_tcp_1702.ovpn |
awk '{sub(/^.*\/|_.*$/, "", $0); print $0 } '
tun987_vpngate_14.38.19.182_tcp_1702.ovpn
$

It should give tun987 as the result, am I wrong at some place?

Regards

>
> Janis

Janis Papanagnou

unread,
Oct 13, 2016, 7:23:16 AM10/13/16
to
On 13.10.2016 13:13, Hongyi Zhao wrote:
> On Thu, 13 Oct 2016 10:36:22 +0200, Janis Papanagnou wrote:
>
>>> Can I combine these two sub() commands into one?
>>
>> Yes, the pipe symbol '|' is the logical OR operator.
>> (It's documented.)
>
> Seems it doesn't work for me:
>
> $ echo /bin/tun987_vpngate_14.38.19.182_tcp_1702.ovpn |
> awk '{sub(/^.*\/|_.*$/, "", $0); print $0 } '
> tun987_vpngate_14.38.19.182_tcp_1702.ovpn
> $
>
> It should give tun987 as the result, am I wrong at some place?

Note that regxeps usually match the longest match, so /_.*$/ will
start at the first _ it finds. You need to rewrite your regexp to
match in a more specific way what you actually want to extract.

Janis

>
> Regards
>
>>
>> Janis
>
>
>
>
>

pop

unread,
Oct 13, 2016, 7:47:03 AM10/13/16
to
Hongyi Zhao wrote on 10/13/2016 6:13 AM:
> On Thu, 13 Oct 2016 10:36:22 +0200, Janis Papanagnou wrote:
>
>>> Can I combine these two sub() commands into one?
>>
>> Yes, the pipe symbol '|' is the logical OR operator.
>> (It's documented.)
>
> Seems it doesn't work for me:
>
> $ echo /bin/tun987_vpngate_14.38.19.182_tcp_1702.ovpn |
> awk '{sub(/^.*\/|_.*$/, "", $0); print $0 } '
> tun987_vpngate_14.38.19.182_tcp_1702.ovpn
> $
>
> It should give tun987 as the result, am I wrong at some place?
>
> Regards
>
>>
>> Janis
>
>
>
>
>
use gsub instead of sub; sub does only one match, gsub continues to scan.

--
Best wishes;
(^\pop/^) -> Mark

Hongyi Zhao

unread,
Oct 13, 2016, 9:51:20 AM10/13/16
to
On Thu, 13 Oct 2016 06:47:00 -0500, pop wrote:

> use gsub instead of sub; sub does only one match, gsub continues to
> scan.

Thanks a lot, it does the trick:

$ echo /bin/tun987_vpngate_14.38.19.182_tcp_1702.ovpn | awk '{gsub(/(^.*
\/|_.*$)/, "", $0); print $0 } '
tun987
$

Regards

Hongyi Zhao

unread,
Oct 14, 2016, 8:33:38 AM10/14/16
to
On Thu, 13 Oct 2016 13:23:14 +0200, Janis Papanagnou wrote:

> Note that regxeps usually match the longest match, so /_.*$/ will start
> at the first _ it finds.

Thanks for you notes, I just want the longest match for my case.

Regards

> You need to rewrite your regexp to match in a
> more specific way what you actually want to extract.
>
> Janis





Janis Papanagnou

unread,
Oct 14, 2016, 8:41:45 AM10/14/16
to
On 14.10.2016 14:33, Hongyi Zhao wrote:
> On Thu, 13 Oct 2016 13:23:14 +0200, Janis Papanagnou wrote:
>
>> Note that regxeps usually match the longest match, so /_.*$/ will start
>> at the first _ it finds.
>
> Thanks for you notes, I just want the longest match for my case.

Yes, I noticed that too late after not having inspected your sample close
enough.

Janis

> [...]

0 new messages