Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.debian.user > #247317
| From | Greg Wooledge <greg@wooledge.org> |
|---|---|
| Newsgroups | linux.debian.user |
| Subject | Re: extract values from a string |
| Date | 2022-04-17 05:00 +0200 |
| Message-ID | <Ed3iN-9fJw-1@gated-at.bofh.it> (permalink) |
| References | <EcGvU-92e4-15@gated-at.bofh.it> <EcT9L-99V9-3@gated-at.bofh.it> <Ed2PL-9fzw-1@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
On Sat, Apr 16, 2022 at 09:27:52PM -0500, Kent West wrote:
> -- data.txt --
> black berry 12
> blue berry 14
> raspberry 9
> huckle berry hound 3
> bare-knuckle sandwich 27
>
> -- test.sh --
> #!/bin/bash
>
> file="data.txt"
>
> while read -r line; do
> productID=$(awk -F' ' '{$NF=""; print $0}' <<< $line)
> price=$(awk -F' ' '{print $NF}' <<< $line)
Two awks...
> retail=$(($price * 2))
> printf "The price of \'$productID\' is \$$price. We will retail it for
> \$$retail.\n"
> done <$file
>
>
> -- $./test.sh --
> The price of 'black berry ' is $12. We will retail it for $24.
> The price of 'blue berry ' is $14. We will retail it for $28.
> The price of 'raspberry ' is $9. We will retail it for $18.
> The price of 'huckle berry hound ' is $3. We will retail it for $6.
> The price of 'bare-knuckle sandwich ' is $27. We will retail it for $54.
... and you still left trailing whitespace on the product names.
Also, your printf is wrong. You're injecting the variable contents into
the format argument. You should be using %s placeholders in the format,
and then passing each variable as a separate argument.
Here's a version that uses zero forks, and removes the trailing
whitespace correctly. It's the same parameter expansion with extended
globs that I showed earlier in this thread.
unicorn:~$ cat foo
#!/bin/bash
shopt -s extglob
while read -r line; do
price=${line##*[[:space:]]}
retail=$((price * 2))
product=${line%%+([[:space:]])"$price"}
printf "The price of '%s' is \$%d. We will retail it for \$%d.\n" \
"$product" "$price" "$retail"
done
unicorn:~$ ./foo < data.txt
The price of 'black berry' is $12. We will retail it for $24.
The price of 'blue berry' is $14. We will retail it for $28.
The price of 'raspberry' is $9. We will retail it for $18.
The price of 'huckle berry hound' is $3. We will retail it for $6.
The price of 'bare-knuckle sandwich' is $27. We will retail it for $54.
Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
extract values from a string wilson <info@bigcount.xyz> - 2022-04-16 04:40 +0200
Re: extract values from a string Greg Wooledge <greg@wooledge.org> - 2022-04-16 05:10 +0200
Re: extract values from a string Nicholas Geovanis <nickgeovanis@gmail.com> - 2022-04-16 18:10 +0200
Re: extract values from a string <tomas@tuxteam.de> - 2022-04-16 18:20 +0200
Re: extract values from a string Nicholas Geovanis <nickgeovanis@gmail.com> - 2022-04-16 18:50 +0200
Re: extract values from a string Greg Wooledge <greg@wooledge.org> - 2022-04-16 19:30 +0200
Re: extract values from a string Kent West <westk@acu.edu> - 2022-04-17 04:30 +0200
Re: extract values from a string Greg Wooledge <greg@wooledge.org> - 2022-04-17 05:00 +0200
Re: extract values from a string David Christensen <dpchrist@holgerdanske.com> - 2022-04-16 21:30 +0200
Re: extract values from a string Ash Joubert <ash@transient.nz> - 2022-04-17 02:40 +0200
Re: extract values from a string Ash Joubert <ash@transient.nz> - 2022-04-17 02:40 +0200
Re: extract values from a string Greg Wooledge <greg@wooledge.org> - 2022-04-17 02:40 +0200
Re: extract values from a string Ash Joubert <ash@transient.nz> - 2022-04-17 02:50 +0200
csiph-web