LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2011, 04:36 AM   #1
Steve W
Member
 
Registered: Mar 2007
Distribution: Linux Mint 18.1
Posts: 520

Rep: Reputation: 44
Small scripting query: lowercase


I have several directories of subtitles for some videos, but all the text for the subtitles in each file is in uppercase, and I would like to convert the files' content to lowercase, all in one go. I found on a website a bash command that would do each file separately:

tr '[:upper:]' '[:lower:]' <input.txt> output.txt

... but of course involves specifying the input name and output name for each file.

I have been trying to do it in a script that would work for all files in the current directory, without having to rename them each time, if that's possible. So far I've got the following, which doesn't work:

#!/bin/bash

for file in $@; do
tr '[:upper:]' '[:lower:]'
done

However, my web searches cannot locate a way of specifying "each file in current directory" in conjunction with the "tr" command, but also without having to rename the file once converted to lowercase. Is it possible, or would the tr command have to create a new file for each converted file?

Thank you for any advice you can give.

Last edited by Steve W; 03-23-2011 at 04:37 AM.
 
Old 03-23-2011, 05:22 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
for file in *.txt
do
    tr '[:upper:]' '[:lower:]' <$file >${file}.new
    mv $file.new $file
done
tr is a stream oriented editor; it can't replace file contents in-place.
You can prob use sed -i ... if you're that bothered about creating tempfiles.
 
Old 03-23-2011, 05:24 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well the first thing to ask is do any of the file names contain spaces? If yes then you will need a while loop as opposed to a for loop which will break the words on whitespace.

As for what to use in the loop, I am sure there are many methods, but the following would work:
Code:
sed -i 's/.*/\L&/' file
Give it a go on a single file to see if you get the desired result first. Should you want a backup of the original file, simply add backup extension to -i, eg -i.bak
 
Old 03-23-2011, 11:53 AM   #4
Steve W
Member
 
Registered: Mar 2007
Distribution: Linux Mint 18.1
Posts: 520

Original Poster
Rep: Reputation: 44
Thank you both for the advice. Files lowercased!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Scripting query grail Linux - General 9 09-16-2009 08:49 PM
Scripting query.. crash_override_me Programming 8 07-23-2007 04:20 AM
cd filenames in lowercase focus1 Linux - General 2 02-13-2007 12:30 PM
Scripting Query sc_3007 Linux - General 4 12-01-2005 02:00 AM
[c++]to lowercase hylke Programming 3 05-16-2004 01:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration