find, if exists then append for file with same name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find, if exists then append for file with same name
# 1  
Old 05-17-2009
find, if exists then append for file with same name

I will have to process multiple files with same name [filename.txt] everyday.
My requirement is:
If on a certain day I see that filename.txt exists then the contents of the filename.txt would be added/append
to the former file contents.Each time it sees the file the content would be added.But the header
should not be added
For ex: There exists this file
filename.txt
First Name|Second Name
John|Marshall

Next time I get the same file
filename.txt
First Name|Second Name
Jack|Dowell

I need the next file to become like
filename.txt
First Name|Second Name
John|Marshall
Jack|Dowell
Can you please suggest something? Thanks in advance.
# 2  
Old 05-17-2009
Hi RubinPat,
How are you writing to a file? $ command >> filename.txt will append the output of the command to an existent file.
$ command > filename.txt will create a new file or overwrite an existent one.
# 3  
Old 05-17-2009
I am not writing it but I need to process the file. I should be more clear.

I have a file named filename.txt already in the folder

Now the requirement is :


Now I get another file with same name filename.txt .


So my code will be like this

if the file with name filename.txt already exists
then
append the contents of the new file into the old file without the header.

I hope im clear.

Since this situation will occur multiple times I beleieve I have to use "for do" loop

I am stuck with the same filename append part.
# 4  
Old 05-18-2009
Hi Rubin can u be a bit clear?
# 5  
Old 05-18-2009
Yes Henry,

I have

filename.txt
Name|Place
a|b

I get
filename.txt
Name|Place
c|d

I need
filename.txt
Name|Place
a|b
c|d

Only there is a condition "for do loop" needs to be used to determine the eistence of the file filename.txt......Any idea
# 6  
Old 05-18-2009
normally , you can't get 2 files with the same name on the same directory..unless you are talking about filename.txt at a different directory.
# 7  
Old 05-18-2009
actually, ghostdog...the scenario is
i will have a file already named filename.txt in the directory

Now when the next comes with the name filename.txt ,

my code has to look
for filename.txt
if exists
then append the contents of new file to the older one [except the header]...

I hope i could explain

Any clue...nyways thanks gostdog
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a file and if exists , execute a different file

Good Morning All, I'm a novice and please excuse me if i did miss some of the forum rules. What my intention is, i have a file (services) residing @ /u01/Oracle/services. I know i can use the find command to find the "service" file. I get this file from a windows box and there is no certain... (8 Replies)
Discussion started by: thinkingeye
8 Replies

2. Shell Programming and Scripting

Append users if GID exists

I have thousands of users assigned various roles. The role header defines the GID that they should have. If I can get all the GID information into /etc/group format I can upload the output file and easily assign all users into the desired GID. Right now I can get an output file like this:... (5 Replies)
Discussion started by: MaindotC
5 Replies

3. Shell Programming and Scripting

How to find out whether a file exists with the help of regular expression?

Hi all I have a list of file names in array. But this file names are not exact. so i want to search whether a file exists or not using regular expression. code snippet: if ; then echo "File exists" else echo "File does not exits" fi over here "*EQST*" should be treated as a regular... (4 Replies)
Discussion started by: Ganesh_more
4 Replies

4. Shell Programming and Scripting

Find string in a file and append character

Hi Experts, Is there a way to find a string in a file then append a character to that string then save the file or save to another file. Here is an example. >cat test.txt NULL NULL NULL 9,800.00 NULL 1,234,567.01 I want to find all NON NULL String and add a dollar sign to those... (9 Replies)
Discussion started by: brichigo
9 Replies

5. UNIX for Dummies Questions & Answers

Statement to find if an entry exists in a file

I need to check if an entry input by the user is in a file. If so, I need to run a command, and if it does not exist then it should output entry does not exist. So I have so far... echo "Enter record:" read record //command || //command Can I use an if statement to do this? (3 Replies)
Discussion started by: itech4814
3 Replies

6. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

7. UNIX for Dummies Questions & Answers

Find, copy, and append into one file

Hi, I am trying to do something relatively easy, but am having some trouble getting it to work. I have multiple files called "distances.log" in numerous subdirectories and sub-subdirectories within a directory. I would like the contents of each of these "distances.log" files to be appended to a... (2 Replies)
Discussion started by: euspilapteryx
2 Replies

8. Solaris

Find a file and xarg mv to append date to file

Hello All, What I would like to do is search for a file and then run a mv command to rename the file to have todays date appended to it. The find when I run it finds eight or so files and I would like to append a date stamp to each file. If possible using one line command would be great. Can... (6 Replies)
Discussion started by: jacktravine
6 Replies

9. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

10. Shell Programming and Scripting

unix script to check whether particular file exists and to find its size

I want to find the size of particular file exists in a particular directory and i wnt to zip it. In the below mentioned code it should check the MQ.log in the particular directory.Please correct my code so that it will check for particular MQ.log but i could not able to check whether the... (9 Replies)
Discussion started by: Balachandar
9 Replies
Login or Register to Ask a Question