How to Capitalize First Letter of Each Line in Bash

Changing the first letter of every line in a file to uppercase

Use sed:

sed  's/^\(.\)/\U\1/' yourfile > convertedfile

Little explanation:

  • the ^ represents the start of a line.
  • . matches any character
  • \U converts to uppercase
  • \( ... \) specifies a section to be referenced later (as \1 in this case); parentheses are to be escaped here.

Do not try to redirect the output to the same file in one command (i.e. > yourfile) as you will lose your data. If you want to replace in the same file then check out joelparkerhenderson's answer.

Capitalize first letter of each line

You can use

sed 's/[[:alpha:]]/\U&/' myfile.txt > convertedfile.txt

Here, s/[[:alpha:]]/\U&/ finds the first letter (with [[:alpha:]]) and it is capitalized with \U operator (& stands for the whole match, the matched letter). As there is no /g flag, only the first occurrence (per line) is affected.

See the online demo:

s='david
Sam
toNy
3matt
5$!john'
sed 's/[[:alpha:]]/\U&/' <<< "$s"

Output:

David
Sam
ToNy
3Matt
5$!John

uppercase first character in a variable with bash

foo="$(tr '[:lower:]' '[:upper:]' <<< ${foo:0:1})${foo:1}"

Change case of first word of each line

Use awk one-liner:

awk -F$'\t' -v OFS=$'\t' '{ $1 = toupper($1) }1' file

Uppercasing First Letter of Words Using SED

This line should do it:

sed -e "s/\b\(.\)/\u\1/g"

Troubleshooting bash script to capitalize first letter in every word

As I said in a comment of the OP, you can use (in Bash≥4) the following:

${var,,}
${var^}

to respectively have the expansion of var in lowercase and var in lowercase with first letter capitalize. The good news is that this also works on each field of an array.

Note. It is not clear from you question whether you need to apply this on each word of a line, or on each line. The following addresses the problem of each word of a line. Please refer to Steven Penny's answer to process only each line.

Here you go, in a much better style!

#!/bin/bash

file=names.txt
echo "#################################"
k=1
while read -r -a line_ary;do
lc_ary=( "${line_ary[@],,}" )
echo "Line # $k: ${lc_ary[@]^}"
((++k))
done < "$file"
echo "Total number of lines in file: $k"

First we read each line as an array line_ary (each field is a word).

The part lc_ary=( "${line_ary[@],,}" ) converts each field of line_ary to all lowercase and stores the resulting array into lc_ary.

We now only need to apply ^ to the array lc_ary and echo it.

Bash: Capitalize first letter of name and surname in .csv file

The original bash code read in and modified the contents of acc.csv, but did not organize or export the modified contents.

The modified code may help:

#!/bin/bash

while IFS=, read -r col1 col2 col3

do
unset col3_new

if [ "$col1" == id ]; then
echo "${col1},${col2},${col3}"
continue
fi

for n in $col3; do
if [ -z "$col3_new" ]
then
col3_new=${n^}
else
col3_new+=" ${n^}"
fi
done
echo "${col1},${col2},${col3_new}"

done < acc.csv > acc_new.csv


Related Topics



Leave a reply



Submit