LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-20-2008, 04:57 PM   #1
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Rep: Reputation: 0
script to change multiple files


I am running out of time so I am asking. I need to run this script (it changes text from lower to upper case)


#!/usr/bin/ksh

awk '

($0 ~ "[a-z]") {
$0=toupper($0);
}

{ printf("%s\n", $0); }

' $1


on 2000 files and I can not figure out what I need to do. I am very new at Linux and my only programming background was a little basic a few years ago. All the examples I can find I end up not understanding what is happening. Could somebody please add the correct code so that I can run this in the directory where the files are stored.
 
Old 07-20-2008, 05:14 PM   #2
nidsche
LQ Newbie
 
Registered: Jun 2008
Location: Germany
Posts: 21

Rep: Reputation: 16
possible solution

Hi,

you have two options:

1) wrapper which calls your script

2) enhance the script.

lets assume your scripts name is change_me.sh

then you could do a wrapper:

#!/usr/bin/ksh

for i in `ls [path to the files]`
do
change_me.sh $i
done

---- End of Script

What happens:

within the for loop you execute an ls command (e.g. ls /path/to/files)
the for loops takes the files as a parameter to i
inside you call your script with the filenames





Quote:
Originally Posted by clstanton View Post
I am running out of time so I am asking. I need to run this script (it changes text from lower to upper case)


#!/usr/bin/ksh

awk '

($0 ~ "[a-z]") {
$0=toupper($0);
}

{ printf("%s\n", $0); }

' $1


on 2000 files and I can not figure out what I need to do. I am very new at Linux and my only programming background was a little basic a few years ago. All the examples I can find I end up not understanding what is happening. Could somebody please add the correct code so that I can run this in the directory where the files are stored.
 
Old 07-20-2008, 06:05 PM   #3
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Original Poster
Rep: Reputation: 0
The usage I was told to use would be:

usage is: mk_upper [filename] > outfile

so I made script:

#!/usr/bin/ksh
for i in `ls /my/directory`
do
mk_upper.txt $i
done

I received this error;
returned bad interpreter: No such file or directory


I have placed both files in /usr/bin
 
Old 07-20-2008, 06:17 PM   #4
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Original Poster
Rep: Reputation: 0
I tried adding $i as output;

#!/usr/bin/ksh
for i in `ls /cl/Dysim_Problems`
do
mk_upper.txt $i > $i
done

Same response

both files in PWD and /usr/bin/
 
Old 07-20-2008, 06:41 PM   #5
nidsche
LQ Newbie
 
Registered: Jun 2008
Location: Germany
Posts: 21

Rep: Reputation: 16
Quote:
Originally Posted by clstanton View Post
The usage I was told to use would be:

usage is: mk_upper [filename] > outfile

so I made script:

#!/usr/bin/ksh
for i in `ls /my/directory`
do
mk_upper.txt $i
done

I received this error;
returned bad interpreter: No such file or directory


I have placed both files in /usr/bin
I was assuming that you have ksh at the place of the hashbang ... so we have to exchange

#!/usr/bin/ksh

please try
#!/bin/bash
instead

also we have to change the wrapper a little bit:

#!/bin/bash
for i in `ls /my/directory`
do
mk_upper.txt /my/directory/$i
done

the wrapper please modify the following way:


#!/bin/bash

target=/my/uppercasetargetdir/`basename $1`

awk '

($0 ~ "[a-z]") {
$0=toupper($0);
}

{ printf("%s\n", $0); }

' $1 > $target


this changes the files to upper case in the targetdirectory /my/uppercasetargetdir/

please do not forget to make a makedir /my/uppercasetargetdir/
 
Old 07-20-2008, 06:48 PM   #6
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
tr command might also do the trick instead of awk (maybe a bit more lightweight solution). It works basically like this:

echo TEXT | tr [:upper:] [:lower:]

see man tr.
 
Old 07-20-2008, 07:30 PM   #7
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Original Poster
Rep: Reputation: 0
I do appreciate your help Nidsche

so here is command line I ran:
./mylooper.txt

contents of mylooper.txt:

#!/bin/bash
for i in `ls /my/directorywithfiles`
do
mk_upper.txt /my/directorywithfiles/$i
done

contents of mk_upper.txt:

#!/bin/bash
target=/my/uppercasetargetdir/`basename $1`
awk '

($0 ~ "[a-z]") {
$0=toupper($0);
}

{ printf("%s\n", $0); }

' $1 > $target

created files in my/uppercasetargetdir
files were empty.
While running screen was showing

cannot open file `/my/directorywithfiles/filename' for reading (No such file or directory)

the script created filenames in new directory so it knows the files are there. There is text in the original files.
 
Old 07-20-2008, 07:47 PM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
The previous tr example might not be right.

Code:
#!/bin/bash
for file in *.txt; do
  tr '[[:lower:]]' '[[:upper:]]' <"$file" >"${file}.new"
  mv "${file}.new "${file}"
done
The wild card may not match the pattern of files you want, but you can easily change that.
I don't use the ksh and don't know how variables are evaluated. But I'm not doing anything
fancy here so it should work for you.
I'm simply looping through every file, converting to uppercase
and saving the file with ".new" appended to them. Then I copy over the original
with the "mv" command. Redirecting the output to the same filename as the input would
cause the file to be zeroed before tr opens it.

Last edited by jschiwal; 07-20-2008 at 07:49 PM.
 
Old 07-20-2008, 08:09 PM   #9
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Original Poster
Rep: Reputation: 0
jschiwal

Thank you. You are certainly a "Steely Eyed Missile Man"
 
Old 07-20-2008, 09:53 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Could I encourage you to use the '.sh' extension for files containing shell cmds, inc awk.
In Linux, file extensions are optional (and binary executables don't normally have one at all), but the convention is that if you use them (and most do), then shell files end in .sh eg
change_me.sh

change_me.txt
would cause others to expect a flat text (data) file.
 
Old 07-21-2008, 12:29 AM   #11
clstanton
LQ Newbie
 
Registered: May 2008
Posts: 27

Original Poster
Rep: Reputation: 0
I will follow that advice, Thank you
 
Old 07-21-2008, 05:52 AM   #12
nidsche
LQ Newbie
 
Registered: Jun 2008
Location: Germany
Posts: 21

Rep: Reputation: 16
Quote:
Originally Posted by clstanton View Post
I do appreciate your help Nidsche

so here is command line I ran:
./mylooper.txt

contents of mylooper.txt:

#!/bin/bash
for i in `ls /my/directorywithfiles`
do
mk_upper.txt /my/directorywithfiles/$i
done

contents of mk_upper.txt:

#!/bin/bash
target=/my/uppercasetargetdir/`basename $1`
awk '

($0 ~ "[a-z]") {
$0=toupper($0);
}

{ printf("%s\n", $0); }

' $1 > $target

created files in my/uppercasetargetdir
files were empty.
While running screen was showing

cannot open file `/my/directorywithfiles/filename' for reading (No such file or directory)

the script created filenames in new directory so it knows the files are there. There is text in the original files.
Could you please check that the account you are working with is allowed to read the files?

e.g. go to /my/directorywithfiles
do
ls -l
there should be something like
-rw-r--r-- username groupname ....something...

first rw indicates that the user might read and write
2nd r means group may read
3rd r means others might read

if something is missing you might have to do a chmod
look at
man chmod
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How can I change the prefix of multiple files in a directory 6millionbucks Programming 13 08-01-2007 12:05 PM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 02:46 AM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 09:16 PM
Using grep to change multiple files zwyrbla Linux - General 5 01-11-2005 03:14 PM
How can i change names of multiple files? phz Linux - General 5 11-15-2003 11:06 AM


All times are GMT -5. The time now is 08:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration