mirror of
https://gitea.com/mcereda/oam.git
synced 2026-02-09 05:44:23 +00:00
chore(snippets): how to print the last field of strings
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
# `awk`
|
# `awk`
|
||||||
|
|
||||||
## Table of contents <!-- omit in toc -->
|
|
||||||
|
|
||||||
1. [TL;DR](#tldr)
|
1. [TL;DR](#tldr)
|
||||||
1. [Further readings](#further-readings)
|
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.
|
# Print only lines where the value of the second column is greater than 100.
|
||||||
awk '$2 > 100' sales.txt
|
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
|
## Further readings
|
||||||
@@ -33,7 +35,8 @@ awk '$2 > 100' sales.txt
|
|||||||
- [The essential Bash cheat sheet]
|
- [The essential Bash cheat sheet]
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
References
|
Reference
|
||||||
|
═╬═Time══
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<!-- Others -->
|
<!-- Others -->
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
# Grep
|
# Grep
|
||||||
|
|
||||||
## Table of contents <!-- omit in toc -->
|
|
||||||
|
|
||||||
1. [TL;DR](#tldr)
|
1. [TL;DR](#tldr)
|
||||||
1. [Variants](#variants)
|
1. [Variants](#variants)
|
||||||
1. [Archive-related variants](#archive-related-variants)
|
1. [Archive-related variants](#archive-related-variants)
|
||||||
1. [PDFgrep](#pdfgrep)
|
1. [PDFgrep](#pdfgrep)
|
||||||
1. [Gotchas](#gotchas)
|
1. [Gotchas](#gotchas)
|
||||||
1. [Further readings](#further-readings)
|
1. [Further readings](#further-readings)
|
||||||
1. [Sources](#sources)
|
1. [Sources](#sources)
|
||||||
|
|
||||||
## TL;DR
|
## TL;DR
|
||||||
|
|
||||||
@@ -33,6 +31,10 @@ find . -type f -print0 | xargs -0 -n 1 -P "$(nproc)" grep 'pattern'
|
|||||||
|
|
||||||
# Highlight numbers in strings.
|
# Highlight numbers in strings.
|
||||||
grep --color '[[:digit:]]' 'file.txt'
|
grep --color '[[:digit:]]' 'file.txt'
|
||||||
|
|
||||||
|
# Only print text after a delimiter.
|
||||||
|
echo "string,with,delimiters" | grep -o '[^,]*$'
|
||||||
|
echo "string/with/delimiters" | grep -o '[^/]*$'
|
||||||
```
|
```
|
||||||
|
|
||||||
## Variants
|
## Variants
|
||||||
@@ -52,8 +54,10 @@ grep --color '[[:digit:]]' 'file.txt'
|
|||||||
|
|
||||||
For simple searches, you might want to use [pdfgrep].
|
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/>
|
Should you need more advanced grep capabilities not incorporated by pdfgrep, you might want to convert the file to text
|
||||||
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]):
|
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
|
```sh
|
||||||
find /path -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "your pattern"' ';'
|
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]
|
- [Grep the standard error stream]
|
||||||
- [`pdfgrep`][pdfgrep]
|
- [`pdfgrep`][pdfgrep]
|
||||||
|
|
||||||
## Sources
|
### Sources
|
||||||
|
|
||||||
All the references in the [further readings] section, plus the following:
|
|
||||||
|
|
||||||
- Answer on [StackOverflow] about [how to search contents of multiple pdf files]
|
- Answer on [StackOverflow] about [how to search contents of multiple pdf files]
|
||||||
- [Regular expressions in grep with examples]
|
- [Regular expressions in grep with examples]
|
||||||
- [Parallel grep]
|
- [Parallel grep]
|
||||||
|
- [How to find the last field using `cut`]
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
References
|
Reference
|
||||||
|
═╬═Time══
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<!-- In-article sections -->
|
|
||||||
[further readings]: #further-readings
|
|
||||||
|
|
||||||
<!-- Knowledge base -->
|
<!-- Knowledge base -->
|
||||||
[grep the standard error stream]: grep%20the%20standard%20error%20stream.md
|
[grep the standard error stream]: grep%20the%20standard%20error%20stream.md
|
||||||
[pdfgrep]: pdfgrep.md
|
[pdfgrep]: pdfgrep.md
|
||||||
|
|
||||||
<!-- Others -->
|
<!-- 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
|
[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/
|
[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/
|
[regular expressions in grep with examples]: https://www.cyberciti.biz/faq/grep-regular-expressions/
|
||||||
|
|||||||
11
snippets/print the last field of strings.sh
Normal file
11
snippets/print the last field of strings.sh
Normal 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
|
||||||
Reference in New Issue
Block a user