Write a row on a CSV

Hello Experts,

I have a log file i.e. a CSV file present in SFTP server.  Now I want to connect to SFTP server using private key then insert a row at end without replacing the file as the same file might be used by other applications.

For example if the CSV should be have 3 rows then 4th row can be written , but there could be a situation like another system will also try to insert another log .

Need batch script .
LVL 17
Swadhin Ray Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
Just for record, I would be hugely surprised, if something like this can be done using FTP or sFTP.
0
Shaun VermaakTechnical SpecialistCommented:
You can use the APPE FTP command but it is not as trivial as just running command, you need to interact with the file on a byte level
https://stackoverflow.com/questions/2536592/use-of-ftp-append-command

How big is the file? I would just download, locally append and upload (overwrite)
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
If your local file just contains that single line to append (or you can create such a file locally), the FTP append command is best, because it uses the least possible command set and time, and is as safe as possible.
We usually rename the remote file, append to it, and then rename back. That makes sure nothing gets lost, but works only if the sequence of lines is not that much of importance.
0
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Swadhin Ray Author Commented:
Thanks for all your comments . Rename the remote file, append to it cannot be done as the same file with same name is accessed by other application for example the same file is used as a external table by Oracle database.  So without renaming the file how to achieve the solution.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
As said, if you cannot use rename/append/rename, you need to trust into the APPEND command. But it might fail, in particular since the target file is in use - which might allow updates or not, depending on the locks applied by Oracle RDMS external table feature. I've not used that (yet), so can't tell, but I assume you can perform updates of the file while it is not in direct use (running a SQL accessing the external table) while you add content.
0
Swadhin Ray Author Commented:
using sed command we were able to append the rows into the same file
0
Swadhin Ray Author Commented:
want to close this question
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I cannot see how sed answers this question at all.
You are talking about a remote file in use by more than one application, and writing to it via (S)FTP - which hints there is no direct file access.
Even if there is direct file access available, sed requires to copy the file to the client, process, and write complete file. sed has no relation to appending data, it is a filtering and processing tool.
0
Swadhin Ray Author Commented:
Sed helps in appending the data on the CSV but accessing the file was done using simple FTP command.
Process involved to solve this issue was to connect the target server using a ftp script and then pass the command to append the required line to the same file without renaming the file or copying the file into the local server.

So the reason for closing this question is did not received any answers against what was used to solve the problem.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You have to use the FTP APPE command as stated by Shaun then. If creating the new row (text line) was part of the question, it has not been obvious, and technically impossible to answer, because we cannot know the process you use.
0
Swadhin Ray Author Commented:
completely agreed but I did mentioned about the process and clearly mentioned that file is already used as an external table and we cannot rename and only way was looking for if we add a row by an existing solution available but I don't think I did received this and the solution what was implemented but let me know if you have the solution then would happy to try out .

Please post the exact code that can be testing having the same file used as external table.
0
Swadhin Ray Author Commented:
also not using sed command please.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Sorry, Swadhin, there seems to be a communication issue.
How you generate the "row" to append on the client does not matter. Just do that  - you should have code for that already, and we could only provide an example for that:
>> C:\temp\logfile.csv echo frst,second,third

Open in new window

Now you connect per SFTP, and run
append C:\temp\logfile.csv \logs\logfile.csv

Open in new window

and then delete the local logfile (as we are appending with >>)
del C:\temp\logfile.csv

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.