[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
And here's lovely sed(1) tip-of-the-day (replace nth occurrence ...)
- To: BerkeleyLUG <berkeleylug@googlegroups.com>
- Subject: And here's lovely sed(1) tip-of-the-day (replace nth occurrence ...)
- From: "Michael Paoli" <Michael.Paoli@cal.berkeley.edu>
- Date: Sun, 12 Jul 2020 08:52:44 -0700
- Arc-authentication-results: i=2; gmr-mx.google.com; spf=neutral (google.com: 198.144.192.42 is neither permitted nor denied by best guess record for domain of michael.paoli@cal.berkeley.edu) smtp.mailfrom=Michael.Paoli@cal.berkeley.edu
- Arc-authentication-results: i=1; gmr-mx.google.com; spf=neutral (google.com: 198.144.192.42 is neither permitted nor denied by best guess record for domain of michael.paoli@cal.berkeley.edu) smtp.mailfrom=Michael.Paoli@cal.berkeley.edu
- Arc-message-signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:reply-to:user-agent :content-disposition:mime-version:subject:to:from:date:message-id :sender:dkim-signature; bh=Nw0fHt68wAKcASioLRNNffKTEigqVv9fYbhzGZstcgk=; b=AL5yjyhWpAXS2+tHXg367HTGLWMHWHIYsxO5/CSPiX9Kj1bRizxWwqoSjHg7220nSY kExT+4k6YffrZTWmwLBSM/PwkXNh3Of+ofAZRzw4XtLt+QslZaA0La9LZ6Ts91H461hq LFl+cfPgrjUrMEmK5f2NlAQGXfOQLqynIeaTOFFc86tLHNKM+NNd6i12Vul3CdX1Uzrm 9tkwE795Vu7PdUDKAl0NqgeoqNjsAfz/m0k6NN9aRzRjZAyIKsQwYKfQrtlQbXMzUmmX DG0D3LnTwSNbrNGkAz1LuX9lNiIvBm6mUUcGn44qeoKDjAQh7xmI88yzw4WojvGFSAM2 XmDA==
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:content-transfer-encoding:content-disposition :mime-version:subject:to:from:date:message-id; bh=0Spm5pCGKAGcIN1nVhG9diYnOZSRf2snkbNIiZ942Hg=; b=aQL442wUITdarjfkfd4wTRd3o6/XNiNoa2KgDG3bhJw2u3cjAersywfauG2h6X6wJP HaCIw47LzmZvhaqWNKjJtNNoh8pgPwQ45EocQVVwQZBUxut/Hw2OpDnIGKp94ZcHHQg1 CQurjmoNZIrU07IyzbpgQtPt+ShWXiudbfQ79232PyFWUh+GbQEIliA3sXSzvvfyGIja ZXtZIlcXtwwBYNPaTX4J8Y1s3hdKs7UVfkz6jb1ZzF9K036L78NQJwdy6JFxL5HlxQgs WzDUanrUGcL/cZ17yXksEUjrmYQLswL2uPTC9/cLltZ7Ski7kgLpssIJfuH1DjJX5Ndc xRQg==
- Arc-seal: i=2; a=rsa-sha256; t=1594569166; cv=pass; d=google.com; s=arc-20160816; b=wlRiNYajLFV0NijqnBeXRTky0WCE21gh00LHIf7ZYtRn6XPlYxjiJ0aIZemzjuOyI8 56yibRfeo/bUkWgcSZzr+YvXac9XrscIO/vPv7CXQuisDsATbt+sedj+oTAhtEOhqf1l WmOCpY6lozV7jRKS0LZmiavBhKSFblhDYBor2yP/aRvgteoUy6PmY+ijHnlv7h38//0c wk6ZTK4TiAdXJQSfgqrPiTePVsz4kx18t9NthD4TxkRXG8LMJnso5cnOoZ6cHx4vgDuw 213EdA1me8MAAmkIzHpd+9xQj5PSPyy5k/wWHZcWuglN/iBr7WMzeLe7XKBoGqEvMEct nYaQ==
- Arc-seal: i=1; a=rsa-sha256; t=1594569166; cv=none; d=google.com; s=arc-20160816; b=kHgjgb/NdFYNkQCfT/x1UJKFM28mZtIroksXFeYH0y4WkWR5MGD/4+2q8Ea5YdoWnR 2124U9fuewnDdqA+iGimKYZmcKTaIGS560f9xfLpMFb+iWUYVyihX6ERbrps4jG6d3V8 Ap6BGZxYpUU2C1dGo+WOOM12s3yM6/RXgAi3IVkyvNE3JwqnhWJKKkWA1j05IrdFRLxs ThYjgdq8A05WPsYx4nDPwSlcyKoIjiXwz5mZY8AXfbGDCDMcosDNPQUUl1AhlcfmllNB tDS0Yqvn2MNSa2LeIQo4zXmrTSgy9+EDspJgQcP/9889DXipMBnZfv8ekdrVfslV0kNR hlJw==
- Delivered-to: historian@entropia.netisland.net
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:message-id:date:from:to:subject:mime-version :content-disposition:user-agent:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:list-post:list-help:list-archive:list-subscribe :list-unsubscribe; bh=Nw0fHt68wAKcASioLRNNffKTEigqVv9fYbhzGZstcgk=; b=TP8sqIV9wxclxdYcMHEgGWwspr/ESi8VVtk7xy5rPIk6rMiOjnEV/msnfv070ZQtUX 76TSSQtPL28Egru7TPDflpBXmE19i6m9baUKwqL7PVvdkJH2vCkZvyV1u8btwz+hPbTv gwlRObxoJmj5vhZA+XEmZ84A0yaoVvNGizvhhVX+rBPCpGcP0+FFOpZQUy+1yvlarDLG xKwiMxT7y8GN3Kq8Pwnz/SylF1V2bRqJ2ajNPg7Xz2YH7gjvv/ky5bNKQ96Tej9OXDDj 0ggelsSnkxm34mHDhesM3KJaU65DgEYImlg96MgUfagDfzGJ3H8SgVcby2Q0PBBhAIWb qxpw==
- List-archive: <https://groups.google.com/group/berkeleylu>
- List-help: <https://groups.google.com/support/>, <mailto:berkeleylug+help@googlegroups.com>
- List-id: <berkeleylug.googlegroups.com>
- List-post: <https://groups.google.com/group/berkeleylug/post>, <mailto:berkeleylug@googlegroups.com>
- List-subscribe: <https://groups.google.com/group/berkeleylug/subscribe>, <mailto:berkeleylug+subscribe@googlegroups.com>
- List-unsubscribe: <mailto:googlegroups-manage+61884646931+unsubscribe@googlegroups.com>, <https://groups.google.com/group/berkeleylug/subscribe>
- Mailing-list: list berkeleylug@googlegroups.com; contact berkeleylug+owners@googlegroups.com
- Reply-to: berkeleylug@googlegroups.com
- Sender: berkeleylug@googlegroups.com
- User-agent: Internet Messaging Program (IMP) H3 (4.2.1-RC1)
So, here's lovely sed(1) tip-of-the day.
Something I quite recently (re?)learned about sed(1).
And it's well in the applicable standard(s)
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
but GNU sed(1) appears can't be bothered to include this on their
man or info pages for sed.
So ...
s/BRE/replacement/flags
one does that in ed, ex, vi, heck, even vim.
And there are various useful flags.
Among them g - global(ly).
no g flag or the like, only the first match is replaced.
include the g flag, all matches on pattern space or applicable
line(s) are replaced.
But ... what if one wants to replace the nth occurrence, where 1 <= n <= 9 ?
Of course n defaulting to 1 if there's no g flag.
"
The value of flags shall be zero or more of:
n
Substitute for the nth occurrence only of the BRE found within the
pattern space.
"
E.g.:
$ echo '1234x6789' | sed -e 's/./5/5'
123456789
$
--
You received this message because you are subscribed to the Google Groups "BerkeleyLUG" group.
To unsubscribe from this group and stop receiving emails from it, send an email to berkeleylug+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/berkeleylug/20200712085244.1370780r84qasmck%40webmail.rawbw.com.