Script to change file name


 
Thread Tools Search this Thread
# 8  
Quote:
Originally Posted by rangarasan
The above script will work with bash. But you have used csh.
Ok it works with bash but not csh. Do you know if you have to declare variables in csh?
# 9  
Two things.

1. Avoid using c shell. It is horrible for coding.
2. Use the following script (rename.sh):

Code:
#!/bin/ksh
if [ -e $1 ]; then
  cp "$1" "$1"_fails
fi

as
Code:
ksh rename.sh abcd

---------- Post updated at 07:40 PM ---------- Previous update was at 07:39 PM ----------

Quote:
Originally Posted by millsy5
Ok it works with bash but not csh. Do you know if you have to declare variables in csh?
I have not worked a lot with c shell. But I guess it is something like:

Code:
set var_name value

# 10  
Quote:
Originally Posted by jawsnnn
Two things.

1. Avoid using c shell. It is horrible for coding.
2. Use the following script (rename.sh):

Code:
#!/bin/ksh
if [ -e $1 ]; then
  cp "$1" "$1"_fails
fi

as
Code:
ksh rename.sh abcd

---------- Post updated at 07:40 PM ---------- Previous update was at 07:39 PM ----------



I have not worked a lot with c shell. But I guess it is something like:

Code:
set var_name value

Thanks jaws using the set before the variable name worked. However if looks like with the c shell that the syntax is different for a lot of things. For the if statement it needs round brackets instead of square ones for example.

By the way your code produces the wrong output if you have a suffix at the end of the filename. I need the _fails before the suffix.

---------- Post updated at 03:51 PM ---------- Previous update was at 03:32 PM ----------

Quote:
Originally Posted by rangarasan
Hi,

Try this one,
Code:
#! /usr/bin/bash
base=$1
if [ -f ${base} ];
then
    mv ${base} ${base/./_fails.}
else
    echo "${base} file not found"
fi

Cheers,
RangaSmilie
Hi Ranga,

How exactly does the last part of the following line work

Code:
    mv ${base} ${base/./_fails.}

It works fine with bash but not with csh. If I understood how it worked then maybe I could rewrite it for csh.

It takes the input filename test1.txt.
It then somehow puts _fails before the .txt.
# 11  
My bad. This one inserts the "_failed" command before the extension.

Code:
#!/bin/ksh
if [ -e $1 ]; then
  ofile=`echo $1 | sed 's/\(.*\)\.\(.*\)/\1_failed.\2/g'`
  cp "$1" "${ofile}"
fi

Hope this helps.
This User Gave Thanks to jawsnnn For This Post:
# 12  
Quote:
Originally Posted by millsy5
It works fine with bash but not with csh. If I understood how it worked then maybe I could rewrite it for csh.
http://www.grymoire.com/Unix/CshTop10.txt
# 13  
Quote:
Originally Posted by jawsnnn
My bad. This one inserts the "_failed" command before the extension.

Code:
#!/bin/ksh
if [ -e $1 ]; then
  ofile=`echo $1 | sed 's/\(.*\)\.\(.*\)/\1_failed.\2/g'`
  cp "$1" "${ofile}"
fi

Hope this helps.
Thanks Jaws. That works exactly as I wanted to but with the ksh. Unfortunately I must use csh or tcsh. I can't get it work with either. Do you mind explaining the logic you used with sed? I'm familiar with sed (I understand the basic use of sed using substitution). But I'm a bit lost with the syntax you have. Hopefully then I can convert it to csh syntax. Thanks again.
# 14  
Your basic global substitution syntax for sed is:

's/search_term/replace_term/g'

This sed command says that:

1. search for any character combination, i.e. \(.*\). Here .* represents any number of occurrences of '.' which is wildcard character and the enclosing \( and \) are used to "mark" the pattern (as explained later)
2. followed by a single dot character, i.e. \. - I used the back slash so sed does not mistake the dot for the wildcard '.' which matches any character
3. followed by - again - any combination of characters - \(.*\) which is marked again.

Now in the replacement area I say:

\1_failed.\2

This basically means that the entire search term will be replaced by the term marked in point 1 (\1) followed by the string "_failed" followed by the term matched in point 3 (\2). This is the benefit of marking (i.e. enclosing search terms in \( and \) ) - you can manipulate the matched patterns around easily. Also notice that I don't need to backslash the '.' character in the replacement area.

Coming to your second problem (converting it to csh syntax), I think the syntax for sed is pretty much universal. You just need to change other parts of the script like the if-else statements to make it work.

While I understand that code in c-shell exists in a lot of systems and has to be maintained - I would like to reiterate: If you have any say in the matter, switch to some other better shell like bash or korn. It is easier to script in, and as neutronscott points out above - there are a lot of reasons to "not" use c shell.
This User Gave Thanks to jawsnnn For This Post:
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #403
Difficulty: Easy
Cygwin is a POSIX-compatible environment that runs natively on Microsoft Windows.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to change file names

I have a landing directory on my unix (solaris) server, that receives the following files: MLH4301I AAOT-hhslog.610.20150805.txt MLH4301I AAOT-hhslog.611.20150805.txt MLH4301I AAOT-hhslog.612.20150805.txt MLH4301I AAOT-hhslog.613.20150805.txt and I need to add to this files the number 10000... (6 Replies)
Discussion started by: fretagi
6 Replies

2. Shell Programming and Scripting

Script to change name of a file with date

I have a file that contains todays date(for ex- test_08/30/2013) Now I want a script which will run and change the name of the file and put yesterday's date (for ex- test_08/29/2013) Please help. ---------- Post updated at 04:40 AM ---------- Previous update was at 04:31 AM ---------- I... (2 Replies)
Discussion started by: sv0081493
2 Replies

3. Shell Programming and Scripting

Script to change the file at one go on different UNIX machines

Hi Folks , I have a query that is I have a server (unix machhine) to which I login thru winscp or putty , mostly I use putty and at a particular location there is one xml(abc.xml) while which I change , let say 1) I login to first unix box hostname :- ccc74 username ... (1 Reply)
Discussion started by: tuntun27272727
1 Replies

4. Shell Programming and Scripting

Change values in .conf file with a script

This is my first time posting here...so be gentle. Suppose I have a test.conf file that contains a value such as a IP. I would like to be able to use the Dialog Utility in Linux to allow me to enter the new IP in a Dialog form...and the results get saved into the test.conf file in the place... (4 Replies)
Discussion started by: calahanp
4 Replies

5. Shell Programming and Scripting

Need script to change a line in file....

Hello all, I have a line of code in a file that I need to change in the /etc/sysconfig/kdump file presently the line reads: KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory mce=off" what I need to do is put a comment out the 1st line and repeat it, and... (5 Replies)
Discussion started by: gartie
5 Replies

6. Shell Programming and Scripting

change file contents using script

Hi, Requirement:- Need to change pfile,so while executing script,it has to go to pfile location(ORACLE_HOME/dbs) and open init<SID>.ora file and change value db_name=<>. If db_name=abcd,script will change the db_name=1234 likr that.. Please help to code this (1 Reply)
Discussion started by: Sanal
1 Replies

7. Shell Programming and Scripting

Help with shell script for know when a file change it

Hi, IŽd like to know how to program a shell script for know when a file changes and based on that make another tasks all this in real time.. Thanks (2 Replies)
Discussion started by: mrios7
2 Replies

8. Shell Programming and Scripting

Need help script to change the log file?

Hi I have log like this : And i want the log become like this : can somebody help me?? (5 Replies)
Discussion started by: justbow
5 Replies

9. Shell Programming and Scripting

script to ftp file (ip change)

Hi All, If I want to ftp files from machine to local pc. But the ip must change everytime :( (due to VPN), can I write a script to easiler (no need to update ip in script)? user_name=aaa password=bbb cat ip.txt # # FTP the files # ftp -d -in 199.200.204.109 <<EOF user $user_name... (2 Replies)
Discussion started by: happyv
2 Replies

10. UNIX for Dummies Questions & Answers

script to change value in file

Hi, I have a parameter file and it contains following items $ cat TransactionParams From_Date_Parm=2005-02-25 To_Date_Parm=2005-05-25 Extract_Root_Parm=/detld1/etl/ascential/Ascential/DataStage/Projects/CTI_London/IAM Extract_Type_Parm=Transaction EDW_Database_Parm=hdw_erks... (1 Reply)
Discussion started by: gopskrish
1 Replies

Featured Tech Videos