![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
look into 'man test'
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
It's easier to read if you use 'CODE' tages around code:
Quote:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|
#7
|
|||
|
|||
|
Code:
if [ ! -s "file" ];then
while read one two
do
echo "$one 0" >> A.txt
done < B.txt
fi
|
|||
| Google The UNIX and Linux Forums |