Adding Shell code to add data to a CSV file

I need a Bash Shell Script that will add multiple lines of text to a CSV file or Text File.
FailureScript.docx
Devoin RuffinAsked:
Who is Participating?
 
nociSoftware EngineerCommented:
A  CSV file is just a tekst file...., all fiels spearated by comma's (thats what the CSV means Comma Separated Values).

you need to enclose strings with ".....  the first line might be a header line with names for you values...

Take notepad and save the next as test.csv
this,is,a,test,tag
1,2,3,4,"1-test"
10,20,30,40,"2-test"
100,200,300,400,"3-test"

Open in new window


Adding a line to it:
echo >>test.csv 1000,2000,3000,4000,'"4-test"'

Open in new window

(The "/' might need some adjust here..., i am not a powershell user)

Now import it into localc, excel ... or any spreadsheet file.
1
 
Devoin RuffinAuthor Commented:
I need this Code be Power Shell code, Also, I will need to be able to add multiple line and be able to delete the file once I have email it. I like this code, but I will need to first check to see if the file exist if so then remove it and then recreate the file
0
 
nociSoftware EngineerCommented:
You can also use bash on windows  through f.e CygWin: https://www.cygwin.com/

and please don's use Microsoft word  or lowrite or the like to edit scripts..
You need to use notepad or other "flat ascii' file editors.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
oBdACommented:
Sorry, no offense, but I'm not opening any Office files.
You can post scripts directly into [code][/code] (see the tool bar above the edit field) tags, or if they're too long, attach them as .txt files.
Errors in the PowerShell console can be posted inside [code][/code] tags as well.
Screen shots can be attached or directly embedded using the tool bar above the Edit field.

That said, in order for us to help you, you need to provide more specific information about what it is exactly that you want to do.
Yes, you can add content to text files, in an abundance of ways, for example using Add-Content.
You can do the same with csv, but in PowerShell. there are better ways to handle csv than treating them as text. Import-Csv will return a csv as a bunch of easily manageable objects with the column names as properties, and Export-Csv will write it back to a file.
Yes, you can check for the existence of files: Test-Path -Path C:\Temp\whatever.txt
Yes, you can delete files: Remove-Item -Path C:\Temp\whatever.txt
0
 
Devoin RuffinAuthor Commented:
Currently I am using the Bash code and the software I am using to edit is called Ultra Edit.

1.) I want to check if the file exist then remove it
2.) Create a New file
3.) Add a new Line for each Job that fails to the csv file.

Below is a sample of the code. I place NEW Code in the area where I will place the code.  $Job_NM is the variable that I will use to populate the CSV file along with the error message

elif  [ $var -eq 4 ]
then
      echo ' Informatica Cloud Job ' $JOB_NM ' is completed with exit code ' $var >>$LOG_FIL_NM
      echo $JOB_NM>>$LOG_FIL_NM
      mv $AUD_FL'.Running' $AUD_FL'.Failed'
NEW CODE WILL GO HERE - CSV FILE CODE
      exit 123
elif  [ $var -eq -1 ]
then
      echo ' Informatica Cloud Job ' $JOB_NM ' is completed with exit code ' $var ' :Error in Utility'>>$LOG_FIL_NM
      echo $JOB_NM>>$LOG_FIL_NM
      mv $AUD_FL'.Running' $AUD_FL'.Failed'
NEW CODE WILL GO HERE - CSV FILE CODE
      exit 123
elif  [ $var -eq 2 ]
then
      echo ' Informatica Cloud Job ' $JOB_NM ' is completed with exit code ' $var ' : No wait specified'>>$LOG_FIL_NM
      echo $JOB_NM>>$LOG_FIL_NM
      mv $AUD_FL'.Running' $AUD_FL'.Failed'
      exit 123
else
      echo ' Status code returned as ' $var>>$LOG_FIL_NM
      echo ' Informatica Cloud Job ' $JOB_NM ' is failed'>>$LOG_FIL_NM
      mv $AUD_FL'.Running' $AUD_FL'.Failed'
NEW CODE WILL GO HERE - CSV FILE CODE
      exit 123
0
 
oBdACommented:
You said you wanted a PowerShell script; that's not PowerShell. And, no offense to Linux, but Bash can't compete with PowerShell. Working with objects beats parsing text just about every time.
So, again: what is it that you want to do (not what you've scripted in bash)?
You seem to start some program and check its errorlevel?
"NEW CODE WILL GO HERE - CSV FILE CODE" isn't enough information. csv is a text based database format (https://en.wikipedia.org/wiki/Comma-separated_values), with the data in columns. You defined exactly nothing about the csv's format, so how can we help you to add rows to it?
0
 
Devoin RuffinAuthor Commented:
Hello
The format should have three columns.

Column 1 -  Counter - this will hold a running counter number
Column 2 - Jobname
Columns 3 - Error - SF_Flg variable.

I want  to create the file once when this code runs. There are 42 Jobs that run thru this process so I will have the create file at the very beginning code
0
 
oBdACommented:
<sigh> This like pulling teeth.
Still unclear, and kind of important, I think: Do you want PowerShell as indicated in your question, or Bash like you posted? Can't help with Bash.
If PowerShell, you still need to provide more information than the Bash snippet provides; at least for a proper solution.
What are those jobs, how are they started, how/where do they get their startup information from?
0
 
Devoin RuffinAuthor Commented:
I place Power Shell in the Header to cover all who would look at this code. I need help with bash code.  The Job is run by this code. I need help with bash code so I do not think you will be able to help

echo ' Informatica Cloud Job: ' $JOB_NM ' started running'>>$LOG_FIL_NM
touch $AUD_FL'.Running'

java -jar RunAJob.jar  $JOB_ID  $JOB_NM $TYPE $FLG >>$LOG_FIL_NM # Run the cloud job
var=$?                                                           # Get the exit status of the previous command
0
 
oBdACommented:
Then don't add the PowerShell topic, and especially don't explicitly write
I need a Power Shell Script
and garnish it with an attached document typically found in Windows environments.
That's really inconsiderate, to the point of being rude.
0
 
Devoin RuffinAuthor Commented:
You know I place the script information in a window document so it would be easier to read. The code is in .sh file and I knew I would not be able to load this file. I was not trying to be rude, but most people work on power shell script will have some back ground in bash coding as well.

You really need to work on your people skill. you should know certain type of file extension will not be loaded and will rejected as spam.

Thank for your time.
0
 
oBdACommented:
As I said above: code snippets can be entered directly using [code][/code] tags ("Style Code" in the tool bar).
PS C:\> "Hello World"

Open in new window

Or you could just have added a .txt extension to the file to upload, since it's a text file anyway.
Both possibilities would have been a lot easier, and a lot more transparent to anyone seeing this question, than creating a Word document from it, where Word might just go ahead and play its games of replacing ASCII quotes with the real purdy curly ones.
Whatever.
0
 
Devoin RuffinAuthor Commented:
I use UltraEdit software for my code review. I was going to load it in a text file but it was very hard to read in a Notepad.
0
 
Devoin RuffinAuthor Commented:
I will be closing this question. The solution that was suggested worked. I was able to create a file and add text.
Thank you so much
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.

All Courses

From novice to tech pro — start learning today.