Trying To Write File Contents To Specfic .csv Cell


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Trying To Write File Contents To Specfic .csv Cell
# 1  
Old 04-18-2014
Trying To Write File Contents To Specfic .csv Cell

Hi,
I'm attempting to write the entire contents of a file to a specific .csv cell. So far have only a nawk one liner that will write a value into a specific .csv cell. Trying to use man page but can't seem to get any farther. Any help would be appreciated.
Code:
nawk -v r=2 -v c=3 -v val=5 -F, 'BEGIN{OFS=","}; NR != r; NR == r {$c = val; print}'

val=5 works but want to write the entire contents of a file to a cell. Hope this question makes sense. Thanks.
# 2  
Old 04-18-2014
Your description is a little bit ambiguous. Maybe this will help.

If the file rep contains:
Code:
this is a two-line
replacement file

and the file csv contains:
Code:
1.1,1.2,1.3,1.4,1.5
2.1,2.2,2.3,2.4,2.5
3.1,3.2,3.3,3.4,3.5

then the script:
Code:
awk -v r=2 -v c=3 '
BEGIN {	FS=OFS=","
}
FNR == NR {
	val = sprintf("%s%s%s", val, NR > 1 ? " " : "", $0)
	next
}
FNR == r {
	$c = val
}
1' rep csv

produces the output:
Code:
1.1,1.2,1.3,1.4,1.5
2.1,2.2,this is a two-line replacement file,2.4,2.5
3.1,3.2,3.3,3.4,3.5

Is this what you're trying to do?
# 3  
Old 04-18-2014
Assuming CSV means Character Separated Variable , NOT, Comma Separated Variable...

Quote:
I'm attempting to write the entire contents of a file to a specific .csv cell.
Some questions.
1) What is the character limit of a cell, assuming there is one?
2) Is the file greater than this limit?
3) Does the file contain any binary?
4) Does the file contain characters the same as the cell field separators?
Or are we to assume a flat alpha-numeric text file only?
# 4  
Old 04-18-2014
Don, the script is returning an error.
Code:
awk: syntax error near line 1
awk: bailing out near line 1

---------- Post updated at 04:55 PM ---------- Previous update was at 04:52 PM ----------

hey wisecracker, not aware of character limit, file is normal text, no character the same as field separators. Thank you.
# 5  
Old 04-18-2014
Quote:
Originally Posted by jimmyf
Don, the script is returning an error.
Code:
awk: syntax error near line 1
awk: bailing out near line 1

Sorry. Since you're using a Solaris system change awk in my script to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 04-19-2014
Thank you kindly, Don. Left you a private message, when you get a moment, please respond? Thanks again!

---------- Post updated 04-19-14 at 01:05 PM ---------- Previous update was 04-18-14 at 06:42 PM ----------

This script it working only I would like to get the text vertically instead of horizontally.
Code:
awk -v r=2 -v c=3 ' BEGIN {	FS=OFS="," } FNR == NR { 	val = sprintf("%s%s%s", val, NR > 1 ? " " : "", $0) 	next } FNR == r { 	$c = val } 1' rep csv

So that the replaced text was like this:
Code:
this is a two-line replacement file

instead of this:
Code:
this is a two-line replacement file


Last edited by Don Cragun; 04-19-2014 at 03:23 PM.. Reason: Fix nested CODE tags.
# 7  
Old 04-20-2014
Quote:
Originally Posted by jimmyf
Thank you kindly, Don. Left you a private message, when you get a moment, please respond? Thanks again!
Please don't use private messages to ask for help. Use the public forums. If you search for awk book or sed book you will find that your question has been asked and answered many times before.
Quote:
Originally Posted by jimmyf
---------- Post updated 04-19-14 at 01:05 PM ---------- Previous update was 04-18-14 at 06:42 PM ----------

This script it working only I would like to get the text vertically instead of horizontally.
Code:
awk -v r=2 -v c=3 ' BEGIN {	FS=OFS="," } FNR == NR { 	val = sprintf("%s%s%s", val, NR > 1 ? " " : "", $0) 	next } FNR == r { 	$c = val } 1' rep csv

If you collapsed the awk script I supplied to a single line (without adding some semicolons), I'm very surprised that it still worked at all.
Quote:
Originally Posted by jimmyf
So that the replaced text was like this:
Code:
this is a two-line replacement file

instead of this:
Code:
this is a two-line replacement file

I don't see any difference in the above two output segments. If you mean that you want the <newline> characters separating lines in the replacement text file to be included in the output, you won't have a valid CSV file unless you quote the <newline>s. With the input files I used before, and the following code:
Code:
/usr/xpg4/bin/awk -v r=2 -v c=3 '
BEGIN {	FS = OFS = ","
}
FNR == NR {
	val = sprintf("%s%s%s", val, NR > 1 ? "\n" : "", $0)
	next
}
FNR == r {
	$c = "\"" val "\""
}
1' rep csv

the output produced is:
Code:
1.1,1.2,1.3,1.4,1.5
2.1,2.2,"this is a two-line
replacement file",2.4,2.5
3.1,3.2,3.3,3.4,3.5

Is this what you wanted?

In the future, please supply sample input and sample output so we can see what you're trying to do.
These 2 Users Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete a columns of a CSV file which has cell values with a string enclosed in " , "?

Hi How can I delete a columns from a CSV file which has comma separated value with a string enclosed in double quotes or square bracket and a comma in between? I have a csv file with below format. Template,Target Server,Target Component,Rule Group,Rule,Rule Reference Number,Rule... (7 Replies)
Discussion started by: Litu19
7 Replies

2. UNIX for Dummies Questions & Answers

Write terminal contents into a one file in UNIX

Hi guys, How to write terminal contents into a file in Unix operating system Actually I created GUI by using Gtk2-perl. I want to display data on GUI whatever the contents writing on terminal. So which command I have to use and where that command to be run I mean in shell script or Perl... (2 Replies)
Discussion started by: kiran425
2 Replies

3. Shell Programming and Scripting

How to delete a column/columns of a CSV file which has cell values with a string enclosed in " , "?

How can I delete a column from a CSV file which has comma separated value with a string enclosed in double quotes and a comma in between? I have a file 44.csv with 4 lines including the header like the below format: column1, column2, column3, column 4, column5, column6 12,455,"string with... (6 Replies)
Discussion started by: dhruuv369
6 Replies

4. Shell Programming and Scripting

Write array contents to file

Hi, I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt. I am using echo ${array} > temp.txt The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
Discussion started by: Filter500
5 Replies

5. Shell Programming and Scripting

Extract last cell of csv file

How do I extract the last cell in a column of a csv file using linux shell scripting? Or alternatively, how do I get the number of cells of a csv file? (2 Replies)
Discussion started by: locoroco
2 Replies

6. Shell Programming and Scripting

Store table contents in csv file

I need to write a script to store the contents of a table in a csv file I'm using Toad, it's a Oracle database. (5 Replies)
Discussion started by: ladyAnne
5 Replies

7. UNIX for Advanced & Expert Users

How to add two values in the same cell of CSV file

I need help to create a csv file with Unix command. In csv file, i need to put two values in the same cell. Rite now, whts happening is, if i put 2 values in the same cell, its comming as " asd, zxc" but i want it in different line but in same cell. asd zxc Please reply me ASAP. (1 Reply)
Discussion started by: Prashant Jain
1 Replies

8. Shell Programming and Scripting

How to edit particular cell of csv file using shell script

I have one csv file and in that I want update particular cell. I know the row and coloumn number for that respective. Please help me... (6 Replies)
Discussion started by: deepak_p86
6 Replies

9. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

10. Programming

How to read and write directory or file contents in c++ ?

Dear Friends, I m beginner unix programmer. I want to know, how to read and write directory or file contents in c++ ? (3 Replies)
Discussion started by: namrata5
3 Replies
Login or Register to Ask a Question