The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check if certain files exist in a directory, if not add name to a textfile SunnyK Shell Programming and Scripting 1 02-07-2008 06:21 AM
Need Script to check file exist and compare rbknisely UNIX for Dummies Questions & Answers 1 01-15-2008 10:08 PM
how to check if directory/file exist using c/c++ steven88 High Level Programming 2 01-02-2006 11:55 PM
how to check if directory/file exist using c/c++ steven88 Shell Programming and Scripting 1 01-02-2006 07:45 PM
how to check if the file exist or not? gusla UNIX for Dummies Questions & Answers 3 03-27-2002 07:56 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-19-2007
Registered User
 

Join Date: Nov 2007
Posts: 131
How to check a file exist and do a copy of other files

Hi,

I would like to perform bash which would check the file A.txt to be size 0 or not. If the size is 0, I would copy file B.txt to replace A.txt.

Please help.

Thanks.


-Jason
Reply With Quote
Forum Sponsor
  #2  
Old 12-19-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
look into 'man test'
Reply With Quote
  #3  
Old 12-19-2007
Registered User
 

Join Date: Nov 2007
Posts: 131
Thanks.

But if i would like to add in a third field with 0 of the intended copied file; how could I do so without creating another files.

Means :-

in MM folder I have A.txt(size which is 0) and in NN folder I have similar A.txt (size more than 0)

if [-s MM/A.txt] then

else cat NN/A.txt <perform the addition of all 0s in third field of A.txt>
copy MM/A.txt to NN/A.txt

I would like to perform the substitution if the size is 0 before copied to the new path.


Thanks.

-Jason

Last edited by ahjiefreak; 12-19-2007 at 07:31 PM.
Reply With Quote
  #4  
Old 12-19-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
It's easier to read if you use 'CODE' tages around code:
Quote:
Originally Posted by ahjiefreak View Post
But if i would like to add in a third field with 0 of the intended copied file; how could I do so without creating another files.

Means :-

in MM folder I have A.txt(size which is 0) and in NN folder I have similar A.txt (size more than 0)
Code:
if [-s MM/A.txt] then

else cat NN/A.txt <perform the addition of all 0s in third field of A.txt> 
copy MM/A.txt to NN/A.txt
I would like to perform the substitution if the size is 0 before copied to the new path.
Last edited by ahjiefreak : 3 Minutes Ago at 03:31 PM.
So NN/A.txt contains some 0's in the third field, and you want to add then up? <confused>
Or do you want to add them to MM/A.txt? Even though it's currently blank? <still confused>

Please show an example of what you are trying to do.

Also, consider using [ ! -s ] so you don't need the 'else' section in your code.
Reply With Quote
  #5  
Old 12-19-2007
Registered User
 

Join Date: Nov 2007
Posts: 131
Hi,

Please disregard the previous said problem as I think I was quite confused by that time.

More clearer picture is below:-

Let us consider we have two files; A.txt and B.txt.

First, I would check the A.txt; if the A.txt is zero size; i would like to copy B.txt content to A.txt but changing all the second field of B.txt content to 0.

Given B.txt input is

1 2
2 3
3 4
1 5
1 6

And output of A.txt would be

1 0
2 0
3 0
1 0
1 0

Currently, I am thinking to use the

if [!- s A.txt]
paste B.txt |awk '{$2=0}' >> A.txt

But it does not work. Please advise. THanks.

-Jason
Reply With Quote
  #6  
Old 12-19-2007
Smiling Dragon's Avatar
Disorganised User
 
Join Date: Nov 2007
Location: New Zealand
Posts: 734
Remember to use 'CODE' tags

You need a 'then' clause in your if statement and to end it with a 'fi'

I don't see a reason to use paste,
just use awk to grab the first column:
Code:
awk '{ print $1,"0" }' < B.txt > A.txt
Reply With Quote
  #7  
Old 12-19-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Code:
if [ ! -s "file" ];then
    while read one two
    do
        echo "$one 0" >> A.txt
    done < B.txt
fi
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:32 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0