chore(snippets): how to print the last field of strings

This commit is contained in:
Michele Cereda
2024-10-25 21:13:17 +02:00
parent c54b2cf9a4
commit 8dad13155f
3 changed files with 31 additions and 15 deletions

View File

@@ -1,7 +1,5 @@
# `awk`
## Table of contents <!-- omit in toc -->
1. [TL;DR](#tldr)
1. [Further readings](#further-readings)
@@ -24,6 +22,10 @@ awk 'length($0) > 20' sales.txt
# Print only lines where the value of the second column is greater than 100.
awk '$2 > 100' sales.txt
# Print only the last column.
echo 'maps.google.com' | awk -F. '{print $NF}'
awk -F '/' '{print $NF}' <<< 'test/with/slashes'
```
## Further readings
@@ -33,7 +35,8 @@ awk '$2 > 100' sales.txt
- [The essential Bash cheat sheet]
<!--
References
Reference
═╬═Time══
-->
<!-- Others -->

View File

@@ -1,14 +1,12 @@
# Grep
## Table of contents <!-- omit in toc -->
1. [TL;DR](#tldr)
1. [Variants](#variants)
1. [Archive-related variants](#archive-related-variants)
1. [PDFgrep](#pdfgrep)
1. [Gotchas](#gotchas)
1. [Further readings](#further-readings)
1. [Sources](#sources)
1. [Sources](#sources)
## TL;DR
@@ -33,6 +31,10 @@ find . -type f -print0 | xargs -0 -n 1 -P "$(nproc)" grep 'pattern'
# Highlight numbers in strings.
grep --color '[[:digit:]]' 'file.txt'
# Only print text after a delimiter.
echo "string,with,delimiters" | grep -o '[^,]*$'
echo "string/with/delimiters" | grep -o '[^/]*$'
```
## Variants
@@ -52,8 +54,10 @@ grep --color '[[:digit:]]' 'file.txt'
For simple searches, you might want to use [pdfgrep].
Should you need more advanced grep capabilities not incorporated by pdfgrep, you might want to convert the file to text and search there.<br/>
You can to this using [pdftotext](pdfgrep.md) as shown in this example ([source][stackoverflow answer about how to search contents of multiple pdf files]):
Should you need more advanced grep capabilities not incorporated by pdfgrep, you might want to convert the file to text
and search there.<br/>
You can to this using [pdftotext](pdfgrep.md) as shown in this example
([source][how to search contents of multiple pdf files]):
```sh
find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "your pattern"' ';'
@@ -76,26 +80,24 @@ find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --
- [Grep the standard error stream]
- [`pdfgrep`][pdfgrep]
## Sources
All the references in the [further readings] section, plus the following:
### Sources
- Answer on [StackOverflow] about [how to search contents of multiple pdf files]
- [Regular expressions in grep with examples]
- [Parallel grep]
- [How to find the last field using `cut`]
<!--
References
Reference
═╬═Time══
-->
<!-- In-article sections -->
[further readings]: #further-readings
<!-- Knowledge base -->
[grep the standard error stream]: grep%20the%20standard%20error%20stream.md
[pdfgrep]: pdfgrep.md
<!-- Others -->
[how to find the last field using `cut`]: https://stackoverflow.com/questions/22727107/how-to-find-the-last-field-using-cut#22727242
[how to search contents of multiple pdf files]: https://stackoverflow.com/a/4643518
[parallel grep]: https://www.highonscience.com/blog/2021/03/21/parallel-grep/
[regular expressions in grep with examples]: https://www.cyberciti.biz/faq/grep-regular-expressions/

View File

@@ -0,0 +1,11 @@
#!/usr/bin/sh
# Ordered best to worst experience
echo "string/with/delimiters" | awk -F '/' '{print $NF}'
echo "string/with/delimiters" | sed 's|^.*/||'
echo "string/with/delimiters" | choose -f '/' -1
echo "string/with/delimiters" | grep -o --color='never' '[^/]*$'
echo "string/with/delimiters" | perl -pe 's|(.*)/(.*)$|$2|' -
echo "string/with/delimiters" | tr '/' '\n' | tail -n1
echo "string/with/delimiters" | rev | cut -d '/' -f '1' | rev