The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 09:21 AM
Need Script to check file exist and compare rbknisely UNIX for Dummies Questions & Answers 1 01-16-2008 01:08 AM
how to check if directory/file exist using c/c++ steven88 High Level Programming 2 01-03-2006 02:55 AM
how to check if directory/file exist using c/c++ steven88 Shell Programming and Scripting 1 01-02-2006 10:45 PM
how to check if the file exist or not? gusla UNIX for Dummies Questions & Answers 3 03-27-2002 10:56 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-19-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
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
  #2 (permalink)  
Old 12-19-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
look into 'man test'
  #3 (permalink)  
Old 12-19-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
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 10:31 PM..
  #4 (permalink)  
Old 12-19-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
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.
  #5 (permalink)  
Old 12-19-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
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
  #6 (permalink)  
Old 12-19-2007
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
  
 

Join Date: Nov 2007
Location: New Zealand
Posts: 922
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:24 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0