So I want a script which provides output as below
I am aware we can use awk/sed to generate below output but not sure how to handle with data inside quotes "".
Please help with script as requested. Many thanks in Advance
I am aware we can use awk/sed to generate below output but not sure how to handle with data inside quotes "".
The idea is simply to define what a "field" is, by defining what separates one such field from the other. There is a variable "IFS" (Internal Field Separator), which does exactly this for Unix lines. Per default it is set to blanks which is why blanks will separate a unix command from its options on the command line. In your case, this separator is "," (comma) and no double quote will interfere once you set the IFS to ",".
The shell command "read var1 [var2 var3 ...]" will read a line, then put the first field to var1, the second to var2, and so on. If there are more fields than variables, the rest of the line goes to the last variable (therefore, if you specify only one everything goes to it), it there are more variables than fields in a line the last variables are empty. You can try that with the following code (i suggest you play around with it a few times to understand the effect:
You should now be ready to understand the following (and eventually adapt it to your needs):
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
My goal in this was to allow the most straight-forward awk statement { print $1,$2,$5,$6 } to solve the problem Of course, as MadeInGermany observed, there are quoted strings in which commas are embedded.
So one thing we could do is to reformat the csv file so that the field separators are something other than commas. This can be done relatively easily with extant perl modules, and we just need to learn how to use them.
So here is a shell script that allows, at the heart, the simple awk print statement noted above:
producing:
Obviously there is little in the way of error checking, but the main body of code works. The intermediate files f1, f2 can be examined to see the results of the transformations.
It's not that simple if the , within the quotes should not be field delimiters.
Especially if their count varies.
Yoda's solution works around it.
Oops! This is true. Somehow the commata inside the double quotes totally escaped my attention. Sorry, this was definitely not the best effort of mine.
In this case I'd like to modify my solution the following way to avoid the problem. The sed-script "masks out" every quoted string from the input. This will work as long as you do not need the content of a quoted string to be in output:
It worked with below script.
but I have a concern. I dont think this will work if data inside the quotes changed
and word count increased in some rows. What if my data goes like this
Dears,I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this. The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the file as an input the file will be... (8 Replies)
Dears,
I am new in shell world and I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this.
The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the... (3 Replies)
hi all i want a script to FTP a file and should generate a quality checksum file
means when I FTP a file from one server to another server it should generate a QC file which should contain timestamp,no.of records in that file
Thanks in advance
saikumar (3 Replies)
Hello;
I need to generate a csv file that contains a list of all the files in a particular server (from the root directory ie: \) that have a permission stamp of 777. I would like to create the csv so that it contains the following:
server name, file name, full path name where file exists,... (17 Replies)
Dear Members,
I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input.
The contents of a single cell of INFO column is like:
Area:app - aam
Clean Up Criteria:... (0 Replies)
Hi Friends
I have an requirement that i need to generate html file using script.
and the script output shold keep adding to that html file like tablewise.
can anyone please help me out in this.
thanks
Krish. (2 Replies)
Hi,
I am new to the shell programming.,
My requirement is , I have an shell file, which call internally the sql file, which generates 4 files on the directory., and then shell has to create the file which contains all the ftp commands to extract the files to different server for later... (1 Reply)
I've seen several examples of scripts in thise forum about having a script generate a log file. I have a script that is run from cron and that monitors a file system for a specfic filename(s) and then performs some actions on them. Normally I call this script from another script (which the one... (2 Replies)