NodeX4
NodeX4

Reputation: 49

One-liner POSIX command to lowercase string in bash

Problem

I have this comand:

sed $((SS - default_scripts))!d customScripts.txt

and it gives me Foo Bar.

I want to convert this to lowercase.

Attempt

When I tried using the | awk '{print tolower($0)}' command on it it returned nothing:

$($(sed $((SS - default_scripts))!d customScripts.txt) | awk '{print tolower($0)}')

Final

Please enlighten me on my typo, or recommend me another POSIX way of converting a whole string to lowercase in a compact manner. Thank you!

Upvotes: 0

Views: 326

Answers (2)

Ed Morton
Ed Morton

Reputation: 204456

Your typo was wrapping everything in $(...) and so first trying to execute the output of just the sed part and then trying to execute the output of the sed ... | awk ... pipeline.

You don't need sed commands nor shell arithmetic operations when you're using awk. If I understand what you're trying to do with this:

$(sed $((SS - default_scripts))!d customScripts.txt) | awk '{print tolower($0)}'

correctly then it'd be just this awk command:

awk -v s="$SS" -v d="$default_scripts" 'BEGIN{n=s-d} NR==n{print tolower($0); exit}' customScripts.txt

Upvotes: 0

Barmar
Barmar

Reputation: 782166

The pipe to awk should be inside the same command substitution as sed, so that it processes the output of sed.

$(sed $((SS - default_scripts))!d customScripts.txt | awk '{print tolower($0)}')

You don't need another command substitution around both of them.

Upvotes: 1

Related Questions