1827807 Members
2671 Online
109969 Solutions
New Discussion

Re: About script writing

 
SOLVED
Go to solution
juno2
Super Advisor

About script writing

I am new comer to use unix to write shell script , could you give me some hints to do that . my situation as below.

The user will generate some .txt files to the system , how can i write a script to change all these .txt to .prn format ?
8 REPLIES 8
Jarle Bjorgeengen
Trusted Contributor
Solution

Re: About script writing

Hi,

do you want to change only the name, or do you want to actually change the contents ? If you want to change the contents, I suggest you use perl. And you you need to understand the format of the prn file, which I'm not familiar with.

If prn is a binary file, you need to other tools than shell commands to do the job.

Rgds Jarle
juno2
Super Advisor

Re: About script writing

thx reply, it is only plain txt files , and only change the file name , no need to change the content. thx
Frederic Sevestre
Honored Contributor

Re: About script writing

Hi,

try this :

for FILE in $(find -name "*.txt")
do
mv $FILE $(dirname $FILE )/$(basename $FILE | cut -d. -f1)
done

Regards,
Fr??d??ric
Crime doesn't pay...does that mean that my job is a crime ?
Jarle Bjorgeengen
Trusted Contributor

Re: About script writing

Right,

try this.

#$/bin/sh

for i in $(ls /your_directory_with_txt_files/*.txt)
do
mv $i /your_directory_with_txt_files/$(basename $i .txt).prn
done

Or you could give the directory as an argument to the script, and replace your_directory_with_txt_files with $1

Rgds Jarle
juno2
Super Advisor

Re: About script writing

Thx all.
John Meissner
Esteemed Contributor

Re: About script writing

scripting is fun (ok... i'm sick but i enjoy it). If you'd like to get some books on it I would recommend.

"Korn Shell Programming by example" by O'Brien, Pitts

&

"sed & awk" by Dougherty & Robbins

for your problem I would write this script:

#! /usr/bin/ksh
for i in $(ls /path/to/files)
do
file=$(echo $i | sed 's/.txt//g')
mv $file $file.txt
done
All paths lead to destiny
ashan
Advisor

Re: About script writing

Hi

If you are new to shell scripting, please buy
this book Hands-On Kronshell93 programming
(ISBN:0-201-31018X other book is Kronshell
Programming Tutorial by Barry Rosenberg

The latest is KSH93 go for it,grate book
by very clear examples.

You can become shell Master.

Good luck

Ashan
twang
Honored Contributor

Re: About script writing