LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-22-2010, 04:05 AM   #1
dhirendrs
Member
 
Registered: Nov 2009
Posts: 32

Rep: Reputation: 15
Need shell script which change the file name with my requirement


i have below file in one directory and i want to rename below file.

Currently :
hid_10_112_97_191.ej.2010071900.1279699203.1279699203.xml
hid_10_112_97_191.ejbank.2010071900.1279699203.1279699203.xml
hid_10_112_97_191.ejdataent.2010071900.1279699203.1279699203.xml
hid_10_112_97_191.ejheader.2010071900.1279699203.1279699203.xml
hid_10_112_97_191.ejitem.2010071900.1279699203.1279699203.xml


After script run it should be.

hid_10_112_97_191.ej.2010071900.xml
hid_10_112_97_191.ejbank.2010071900.xml
hid_10_112_97_191.ejdataent.2010071900.xml
hid_10_112_97_191.ejheader.2010071900.xml
hid_10_112_97_191.ejitem.2010071900.xml


I try to write the script which is below.
it is giving error

#! /bin/sh
MAXLEN=57
MAXLEN1=61
MAXLEN2=62
MAXLEN3=63
MAXLEN4=64
MAXLEN5=65
MAXLEN6=66
MAXLEN7=67
MAXLEN8=68

for filename in *.xml
do
if [${#filename} -eq $MAXLEN]
then
newname=${filename:0:31}
mv "${filename}" "${newname}.xml"
fi
done
 
Old 07-22-2010, 04:26 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,944
Blog Entries: 54

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
Code:
MAXLEN=31; find . -type f -iname \*.xml | while read filename; do [ ${#filename} -ge ${MAXLEN} ] && echo "mv "${filename}" "${filename:0:${MAXLEN}}.xml""; done
?
 
Old 07-22-2010, 04:29 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Is this what you are looking for:

for THIS in *xml; do echo ${THIS} | awk -F. '{ print "mv "$0" "$1"."$2"."$3"."$6 }' | bash ; done

Hope this helps.
 
1 members found this post helpful.
Old 07-22-2010, 05:22 AM   #4
dhirendrs
Member
 
Registered: Nov 2009
Posts: 32

Original Poster
Rep: Reputation: 15
it done and working fine

it is working fine....
 
Old 07-22-2010, 05:25 AM   #5
dhirendrs
Member
 
Registered: Nov 2009
Posts: 32

Original Poster
Rep: Reputation: 15
giving error buddy

Quote:
Originally Posted by unSpawn View Post
Code:
MAXLEN=31; find . -type f -iname \*.xml | while read filename; do [ ${#filename} -ge ${MAXLEN} ] && echo "mv "${filename}" "${filename:0:${MAXLEN}}.xml""; done
?
it is giving error
 
Old 07-22-2010, 08:48 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by dhirendrs View Post
it is working fine....
You're welcome
 
1 members found this post helpful.
Old 07-22-2010, 11:55 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,944
Blog Entries: 54

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
Quote:
Originally Posted by dhirendrs View Post
it is giving error
I don't see no errors?
 
  


Reply

Tags
script, shell


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
using an shell script how to change a value in a file according to the user input badrinath_tcs Linux - Software 6 09-28-2009 07:29 PM
URGENT----shell script to change word in file raghupal Programming 14 10-21-2008 12:49 AM
Can't change shell in plink shell script? lhouk Programming 2 08-02-2007 02:47 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
Change batch script to shell script alan.belizario Programming 5 03-31-2005 12:41 AM


All times are GMT -5. The time now is 10:16 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration