From 8dad13155f728a1d44b21bbf047dc048ae101253 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Fri, 25 Oct 2024 21:13:17 +0200 Subject: [PATCH] chore(snippets): how to print the last field of strings --- knowledge base/awk.md | 9 ++++--- knowledge base/grep.md | 26 +++++++++++---------- snippets/print the last field of strings.sh | 11 +++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 snippets/print the last field of strings.sh diff --git a/knowledge base/awk.md b/knowledge base/awk.md index ab9c390..9d760cf 100644 --- a/knowledge base/awk.md +++ b/knowledge base/awk.md @@ -1,7 +1,5 @@ # `awk` -## Table of contents - 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] diff --git a/knowledge base/grep.md b/knowledge base/grep.md index 8562fc5..2c8fd40 100644 --- a/knowledge base/grep.md +++ b/knowledge base/grep.md @@ -1,14 +1,12 @@ # Grep -## Table of contents - 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.
-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.
+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`] - -[further readings]: #further-readings - [grep the standard error stream]: grep%20the%20standard%20error%20stream.md [pdfgrep]: pdfgrep.md +[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/ diff --git a/snippets/print the last field of strings.sh b/snippets/print the last field of strings.sh new file mode 100644 index 0000000..bcf7582 --- /dev/null +++ b/snippets/print the last field of strings.sh @@ -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