Home > computers > linux > bash > BashStringOperations | About

Extract part of a string using globs

There's a left-to-right extraction with '#' and a right-to-left extraction with '%'

Left-to-right (ltr) is with '#' (which is on the left part of the keyboard, mnemonic) Right-to-left (rtl) is with '%' (which is on the right part of the keyboard, mnemonic)

In both extractions, you either go to the first occurence of the pattern with a single character (wether # or %), or to the last occurence with two characters (## or %%).

For example, let's say I want to keep the first part of an email address (the name of the inbox, without the domain) I would do :

email  = "yassine.chaouche@algerian-radio.dz"
maildir="algerian-radio.dz/${email%@*}"
echo $maildir
algerian-radio.dz/yassine.chaouche

This will place the cursor at the end of the string, and gets rid of everything until it gets to the first occurence of @, which is yassine.chaouche

Extract part of a string using indexes

Use ":" to indicate the part of the string you want to trim. Eg. :

Without the **:**
root@messagerie-secours[10.10.10.20] ~ # while read -r f; do echo here is f : "$f"; done <<<"$(ls -1 /etc/rc2.d/)"
here is f : README
here is f : S14portmap
here is f : S15nfs-common
here is f : S17fancontrol
here is f : S17rsyslog
here is f : S17saslauthd
here is f : S17sudo
here is f : S18acpid
here is f : S18amavis
here is f : S18atd
here is f : S18bind9
here is f : S18clamav-daemon
here is f : S18courier-authdaemon
here is f : S18courier-imap
here is f : S18courier-imap-ssl
here is f : S18courier-pop
here is f : S18courier-pop-ssl
here is f : S18dbus
here is f : S18fail2ban
here is f : S18jailkit
here is f : S18mysql
here is f : S18ntp
here is f : S18openbsd-inetd
here is f : S18quotarpc
here is f : S18snmpd
here is f : S18spamassassin
here is f : S18ssh
here is f : S18webmin
here is f : S19apache2
here is f : S20bootlogs
here is f : S20clamav-freshclam
here is f : S20cron
here is f : S20exim4
here is f : S20postfix
here is f : S20pure-ftpd-mysql
here is f : S20rsync
here is f : S21rc.local
here is f : S21rmnologin
here is f : S21stop-bootlogd
root@messagerie-secours[10.10.10.20] ~ #
With the **:**

So f here contains the service name prefixed with something like S20 or S19. Let's say I want to get rid of the 3 letters prefix, then I would use "${f:3}", like this :

root@messagerie-secours[10.10.10.20] ~ # while read -r f; do echo here is f : "${f:3}"; done <<<"$(ls -1 /etc/rc2.d/)"
here is f : DME
here is f : portmap
here is f : nfs-common
here is f : fancontrol
here is f : rsyslog
here is f : saslauthd
here is f : sudo
here is f : acpid
here is f : amavis
here is f : atd
here is f : bind9
here is f : clamav-daemon
here is f : courier-authdaemon
here is f : courier-imap
here is f : courier-imap-ssl
here is f : courier-pop
here is f : courier-pop-ssl
here is f : dbus
here is f : fail2ban
here is f : jailkit
here is f : mysql
here is f : ntp
here is f : openbsd-inetd
here is f : quotarpc
here is f : snmpd
here is f : spamassassin
here is f : ssh
here is f : webmin
here is f : apache2
here is f : bootlogs
here is f : clamav-freshclam
here is f : cron
here is f : exim4
here is f : postfix
here is f : pure-ftpd-mysql
here is f : rsync
here is f : rc.local
here is f : rmnologin
here is f : stop-bootlogd
root@messagerie-secours[10.10.10.20] ~ #

contact : @ychaouche yacinechaouche at yahoocom


QR Code
QR Code Extract part of a string using globs (generated for current page)