how do I convert the first letter of every word in

2019-09-22 07:31发布

问题:

This question already has an answer here:

  • How to downcase the first character of a string? 8 answers

how do I convert the first letter of each of the below from upper case to lowere case?

For example SentMessage would become sentMessage and NotAttemptedCreditLimitReached would become notAttemptedCreditLimitReached ... etc

list of words below. They are just in a text file.

SentMessage
DeliverySucceeded
DeliveryFailed
DeliveryTimedOut
NotAttemptedCreditLimitReached
NotAttemptedChargingFailure
SubscriptionFailed
SentReportSMDeliveryStatus
FailedToSendReportSMDeliveryStatus
ReportSMSucceeded
ReportSMFailed

Was looking at doing it notepadd++ initially, but open to other suggestions bash/python ... etc


EDIT1 - the solution

$ cat testw
SentMessage
DeliverySucceeded
DeliveryFailed
DeliveryTimedOut
NotAttemptedCreditLimitReached
NotAttemptedChargingFailure
SubscriptionFailed
SentReportSMDeliveryStatus
FailedToSendReportSMDeliveryStatus
ReportSMSucceeded
ReportSMFailed
$ sed -i 's/[A-Z]/\L&/' testw
$ cat testw
sentMessage
deliverySucceeded
deliveryFailed
deliveryTimedOut
notAttemptedCreditLimitReached
notAttemptedChargingFailure
subscriptionFailed
sentReportSMDeliveryStatus
failedToSendReportSMDeliveryStatus
reportSMSucceeded
reportSMFailed

回答1:

If you have gnu sed then use:

sed -i 's/[A-Z]/\L&/' file
  • [A-Z] will match first upper case letter
  • & is back-reference of matched string by pattern (in this case single upper case letter)
  • \L converts given back-reference to lowercase