Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add new function to pull conditional records from a delimited table. #43

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ See something incorrectly described, buggy or outright wrong? Open an issue or s
* [Trim all white-space from string and truncate spaces](#trim-all-white-space-from-string-and-truncate-spaces)
* [Use regex on a string](#use-regex-on-a-string)
* [Split a string on a delimiter](#split-a-string-on-a-delimiter)
* [Match split select on a delimiter](#match-split-select-on-a-delimiter)
* [Change a string to lowercase](#change-a-string-to-lowercase)
* [Change a string to uppercase](#change-a-string-to-uppercase)
* [Trim quotes from a string](#trim-quotes-from-a-string)
Expand Down Expand Up @@ -328,6 +329,38 @@ is
john
```

## Match split select on a delimiter

This is an extention to "Split string on a delimiter" and is meant to more closely mimic the basic functionality of AWK.

**Example Function:**

```sh
cmdparse() {
if [[ -z $1 ]] || [[ $1 == help ]] || [[ -z $2 ]] || [[ -z $3 ]] || [[ -z $4 ]]; then
printf '%s\n' "USAGE: cmdparse \"<command>\" \"<match_substring_list>\" \"<selected_column>\" \"<field_delimiter>\""
return
fi
read -d $'\s' -ra match_list <<< "${2}"
while read -d $'\n' -r line; do
read -d "" -ra field <<< "${line//$4/$'\n'}"
for match in "${match_list[@]}"
do
if [[ $line =~ $match ]]; then
printf '%s\n' "${field[$3]}"
fi
done
done < <($1)
}
```

**Example Usage:**

```shell
$ cmdparse "free" "Mem" 2 $'\t'
3821820
```

## Change a string to lowercase

**CAVEAT:** Requires `bash` 4+
Expand Down