linux bash, camel case string to separate by dash
You can use s/\([A-Z]\)/-\L\1/g
to find an upper case letter and replace it with a dash and it's lower case. However, this gives you a dash at the beginning of the line, so you need another sed expression to handle that.
This should work:
sed --expression 's/\([A-Z]\)/-\L\1/g' \
--expression 's/^-//' \
<<< "MyDirectoryFileLine"
bash - dash case to camel case
Considering that your actual Input_file(data) is same as shown samples if yes then following awk
(s) may help you here.
echo "foo-bar-baz" |
awk -F"-" '{for(i=1;i<=NF;i++){$i=toupper(substr($i,1,1)) substr($i,2)}} 1' OFS=""
Solution 2nd: With use of RS
, FS
and ORS
in awk
.
echo "foo-bar-baz" |
awk 'BEGIN{FS="";RS="-";ORS=""} {$0=toupper(substr($0,1,1)) substr($0,2)} 1'
Spinal Case to Camel Case
GNU sed
This works with GNU sed:
sed -r 's/(^|-)(\w)/\U\2/g'
Match the start of the line or a -
followed by an alphanumeric character and use \U
to make the character uppercase.
And here's how you can operate on a variable with it and assign the result to another variable:
name_upper=$(sed -r 's/(^|-)(\w)/\U\2/g' <<<"$name_spinal")
Perl
It's almost identical in perl:
perl -pe 's/(^|-)(\w)/\U$2/g'
Native bash
Just for fun, here's a way you could do it in native bash:
spinal_to_upper() {
IFS=- read -ra str <<<"$1"
printf '%s' "${str[@]^}"
}
spinal_to_upper "some-string-like-this"
Convert camelcase to lower and underscore_case using bash commands
$ sed 's/^[[:upper:]]/\L&/;s/[[:upper:]]/\L_&/g' <<< 'TestCamelCase'
test_camel_case
\L
is a GNU sed
extension that turns the replacement to lowercase.
linux shell title case
a GNU sed one-liner
echo something-that-is-hyphenated |
sed -e 's/-\([a-z]\)/\u\1/g' -e 's/^[a-z]/\u&/'
\u
in the replacement string is documented in the sed manual.
Convert folder and file names to camel case
You can use the regular expression aptitude to deal with upper and lower case translations, regarding your current local collation (LC_ALL
, check with the locale
command).
If your filename's "words" are separated with a space and are all in lower case, you can use a simple shell script like this :
#!/bin/sh
while read -r FILENAME ; do
NEWNAME="`echo \"${FILENAME}\" | sed 's/ *\([^ ]\)/\u\1/g'`"
if [ ! "${NEWNAME}" ] ; then
NEWNAME="${FILENAME}";
fi
if [ "${FILENAME}" = "${NEWNAME}" ]; then
printf "No change : %s\\n" "${FILENAME}" >&2;
else
if [ -e "${NEWNAME}" ] ; then
printf "Already changed : %s => %s\\n" "${FILENAME}" "${NEWNAME}" >&2;
else
echo "mv \"${FILENAME}\" \"${NEWNAME}\"";
fi
fi
done
Remove the echo
on echo "mv \"${FILENAME}\" \"${NEWNAME}\"";
to do the mv.
Note that it should work fine with accented letters or any unicode letter having lower and upper code.
The script takes the file list to operate from stdin
, so to use it "as is", you can use something like the following examples :
find . -type 'f' | theScript.sh
For a whole tree of files.
For folders, you'll have to operate them separately. List them and sort them in a descending order.ls -1 | theScript.sh
For files in the current folder.
If your files may have all or partial upper cases at start and you look to force them entirely to camel case, you can change the line :
NEWNAME="`echo \"${FILENAME}\" | sed 's/ *\([^ ]\)/\u\1/g'`"
With:
NEWNAME="\`echo \"${FILENAME}\" | sed 's/\(.*\)/\l\1/;s/ *\([^ ]\)/\u\1/g'\`"
generate snake lower case outputs from bash scripts
I was able to get using
sed -r '/symbol/ s/([a-z])([A-Z])/\1_\L\2/g' | awk '/^\{*[[:blank:]]*"symbol[s]?"[[:blank:]]*:/{ $0=tolower($0);}1'''
Thanks everyone for your efforts.
How to rename files to param-case in Bash
Below is an option in bash:
for file in ./* ; do mv "$file" "$(echo $file | sed 's/\(.\)\([A-Z]\)/\1-\2/g' | tr '[:upper:]' '[:lower:]')" ; done
An alternative with perl:
for file in ./* ; do mv "$file" "$(echo $file | perl -ne 'print lc(join("-", split(/(?=[A-Z])/)))')" ; done
Awk/sed script to convert a file from camelCase to underscores
You could use sed also.
$ echo 'fooBar' | sed -r 's/([a-z0-9])([A-Z])/\1_\L\2/g'
foo_bar
$ echo 'fooBar' | sed 's/\([a-z0-9]\)\([A-Z]\)/\1_\L\2/g'
foo_bar
Related Topics
Get String Length in Inline Gnu Assembler
Identify User in a Bash Script Called by Sudo
How to Get Position of Cursor in Terminal
Differencebetween Buffer and Cache Memory in Linux
Are There Standards for Linux Command Line Switches and Arguments
How to Tell Linux Not to Swap Out a Particular Processes' Memory
Find and Delete File or Folder Older Than X Days
Gem5 Syscall Emulation Arm C Hello World Fails with "Fatal: Syscall Gettid (#224) Unimplemented"
Unix: How to Delete Files Listed in a File
Linux: Find All Symlinks of a Given 'Original' File? (Reverse 'Readlink')
How to Copy the Output of a Command Directly into My Clipboard
Difference Between Pid and Tid
Self Modifying Code Always Segmentation Faults on Linux
How to Get the Start Time of a Long-Running Linux Process
Meaning of Tilde in Linux Bash (Not Home Directory)
User-Data Scripts Is Not Running on My Custom Ami, But Working in Standard Amazon Linux
Redirecting Stdout with Find -Exec and Without Creating New Shell