Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Shell script to copy file

Shell Programming and Scripting


Tags
sed

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-14-2011
hainguyen1402 hainguyen1402 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 1 March 2011, 5:01 AM EST
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Shell script to copy file

Dear all,
I have a database with thousands of files with the structure of name is:
Filename_hour_year.abc
Filename_hour_year_1.abc
..............
So what I need is how to write a script that all file with contain the character "_1" will copy to "_2"
For example: file name: abc_111111_2011_1.abc will be copied to abc_111111_2011_2.abc
Thank all!!!
Sponsored Links
    #2  
Old Unix and Linux 02-14-2011
amitranjansahu's Unix or Linux Image
amitranjansahu amitranjansahu is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 6 February 2014, 9:06 AM EST
Location: Bangalore,INDIA
Posts: 507
Thanks: 4
Thanked 27 Times in 27 Posts
PHP Code:
for filename in `ls *_1*`;
do
    
newname=`echo $filename | sed -e 's/_1/_2/g'`
    
mv $filename $newname
done 
Sponsored Links
    #3  
Old Unix and Linux 02-14-2011
hainguyen1402 hainguyen1402 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 1 March 2011, 5:01 AM EST
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Thank Amitranjansahu so much,
It works perfectly...!!
    #4  
Old Unix and Linux 02-14-2011
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 September 2016, 8:57 AM EDT
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Quote:
Originally Posted by hainguyen1402 View Post
Thank Amitranjansahu so much,
It works perfectly...!!
I don't think so.

Code:
echo 'abc_111111_2011_1.abc' | sed -e 's/_1/_2/g'

gives as output:

Code:
abc_211111_2011_2.abc

instead of:

Code:
abc_111111_2011_2.abc

Try:

Code:
for file in *_1.abc
do
    newname=${file%_*}_2.abc
    mv $file $newname
done


Last edited by Franklin52; 02-14-2011 at 05:17 AM.. Reason: typo
Sponsored Links
    #5  
Old Unix and Linux 03-01-2011
hainguyen1402 hainguyen1402 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 1 March 2011, 5:01 AM EST
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry all about my unclear question that I gave.
My problem is copy all files file name have character "_1." to "_2." don't care about extension.
For example: I have files:
ads_daily_10_2008_1.MYD
ads_daily_10_2008_1.MYI
ads_daily_10_2008_1.frm
I need script that can copy these files to new files like:
ads_daily_10_2008_2.MYD
ads_daily_10_2008_2.MYI
ads_daily_10_2008_2.frm
Thank in advance.!

HaiNguyen
Sponsored Links
    #6  
Old Unix and Linux 03-01-2011
yinyuemi yinyuemi is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 24 March 2015, 6:12 AM EDT
Posts: 436
Thanks: 9
Thanked 106 Times in 105 Posts

Code:
 echo "ads_daily_10_2008_1.MYD
ads_daily_10_2008_1.MYI
ads_daily_10_2008_1.frm" |awk '{print "cp "$0" "gensub("_1","_2",$0)}' |sh

Sponsored Links
    #7  
Old Unix and Linux 03-01-2011
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 22 September 2016, 3:57 AM EDT
Location: IN
Posts: 894
Thanks: 15
Thanked 183 Times in 174 Posts
Quote:
Originally Posted by hainguyen1402 View Post
Sorry all about my unclear question that I gave.
My problem is copy all files file name have character "_1." to "_2." don't care about extension.
For example: I have files:
ads_daily_10_2008_1.MYD
ads_daily_10_2008_1.MYI
ads_daily_10_2008_1.frm
I need script that can copy these files to new files like:
ads_daily_10_2008_2.MYD
ads_daily_10_2008_2.MYI
ads_daily_10_2008_2.frm
Thank in advance.!

HaiNguyen
What is wrong with the solution provided in post# 2? It's almost close to your requirement. With some few modifications however..

Code:
for filename in `ls *_1.*`;
do
    newname=`echo $filename | sed 's/_1\./_2./'`
    cp $filename $newname
done


Last edited by michaelrozar17; 03-01-2011 at 03:10 AM.. Reason: removed -e in sed
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to copy a log file if it exceeds 5000000 bytes lookinginfo Shell Programming and Scripting 6 01-03-2011 11:25 PM
Solaris KSH shell script to copy all lines from one file to another ZigZaggin Shell Programming and Scripting 5 06-04-2010 06:23 PM
Shell Script - Copy File at intervals angshuman_ag Shell Programming and Scripting 2 03-18-2010 06:30 AM
Help with a shell script to modify one line and copy the next 9 to same file Teroc Shell Programming and Scripting 5 10-30-2009 07:38 AM
Shell script to search for text in a file and copy file imeadows UNIX for Dummies Questions & Answers 9 11-12-2008 08:12 PM



All times are GMT -4. The time now is 09:50 AM.