Issue
Is there a way to use sed (with potential other command) to transform all the keys in a file that lists key-values like that :
a.key.one-example=a_value_one
a.key.two-example=a_value_two
and I want that
A_KEY_ONE_EXAMPLE=a_value_one
A_KEY_TWO_EXAMPLE=a_value_two
What I did so far :
sed -e 's/^[^=]*/\U&/'
it produced this :
A.KEY.ONE-EXAMPLE=a_value_one
A.KEY.TWO-EXAMPLE=a_value_two
But I still need to replace the "." and "-" on left part of the "=". I don't think it is the right way to do it.
Solution
It should be done very easily done in awk
. awk
is the better tool IMHO for this task, it keeps it simple and easy.
awk 'BEGIN{FS=OFS="="} {$1=toupper($1);gsub(/[.-]/,"_",$1)} 1' Input_file
Simple explanation:
- Make field separator and output field separator as
=
- Then use
awk
's default function namedtoupper
which will make $1(first field) upper case and save it into $1 itself. - Using
gsub
to substitute.
OR-
with_
in $1 as per requirement. - use
1
which is idiomatic way to print a line inawk
.
Answered By - RavinderSingh13 Answer Checked By - Katrina (WPSolving Volunteer)