• Status: Solved
  • Priority: High
  • Security: Private
  • Views: 102
  • Last Modified:

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 .
0
Swadhin Ray
Asked:
Swadhin Ray
2 Solutions
 
Nitin SontakkeDeveloperCommented:
Just for record, I would be hugely surprised, if something like this can be done using FTP or sFTP.
0
 
Shaun VermaakTechnical Specialist/DeveloperCommented:
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
 
QlemoBatchelor, 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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Swadhin RaySenior Technical Engineer 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
 
QlemoBatchelor, 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 RaySenior Technical Engineer Author Commented:
using sed command we were able to append the rows into the same file
0
 
Swadhin RaySenior Technical Engineer Author Commented:
want to close this question
0
 
QlemoBatchelor, 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 RaySenior Technical Engineer 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
 
QlemoBatchelor, 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 RaySenior Technical Engineer 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 RaySenior Technical Engineer Author Commented:
also not using sed command please.
0
 
QlemoBatchelor, 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now