How to edit particular cell of csv file using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to edit particular cell of csv file using shell script
# 1  
Old 04-06-2009
Error 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...
# 2  
Old 04-06-2009
To get a precise answer post a short example and use CODE tags please.
# 3  
Old 04-06-2009
.csv is like something

data1,1,2,3,4
data2,1,2,3,3

now in 2nd row I want to change 2 to 5....

Required one is

data1,1,2,3,4
data2,1,5,3,3

Thanks
zaxxon for updating me...
# 4  
Old 04-06-2009
Quote:
Originally Posted by deepak_p86
.csv is like something

data1,1,2,3,4
data2,1,2,3,3

now in 2nd row I want to change 2 to 5....

Required one is

data1,1,2,3,4
data2,1,5,3,3
r is the row (the line number in the file)
c is the column number
v is the replacement value

This will replace column 3, row 2 with 5, no matter what the value of the cell:

Code:
nawk -v r=2 -v c=3 -v val=5 -F, 'BEGIN{OFS=","}; NR != r; NR == r {$c = val; print}' mydata

p is the previous value

This will replace only if the current value of the cell is equal to p

Code:
nawk -v r=2 -v c=3 -v p=2 -v val=5 -F, 'BEGIN{OFS=","}; NR != r; NR == r {if($r == p){$r = val}; print}' mydata

# 5  
Old 04-06-2009
I have use ur suggesting but I m getting an error regarding file... File is in current directory..

awk -v r=15 19 -v c=7 -v val=96 -F, 'BEGIN{OFS=","}; NR != r; NR == r {$c = val; print}' OFED_coverage.csv
awk: cmd. line:2: fatal: cannot open file `-v' for reading (No such file or directory)
# 6  
Old 04-06-2009
Deepak,
Quote:
Originally Posted by deepak_p86
I have use ur suggesting but I m getting an error regarding file... File is in current directory..

awk -v r=15 19 -v c=7 -v val=96 -F, 'BEGIN{OFS=","}; NR != r; NR == r {$c = val; print}' OFED_coverage.csv
awk: cmd. line:2: fatal: cannot open file `-v' for reading (No such file or directory)
1. Please put CODE tags around Unix stuff (see the reply by zaxxon, a Mod). It makes it easier to read.

2. Please check your command (see below, my emphasis)

Code:
awk -v r=15 19 -v c=7 -v val=96 -F,


Last edited by Beast Of Bodmin; 04-06-2009 at 07:59 AM.. Reason: s/tage/tage/
# 7  
Old 04-06-2009
Code:
open $fh,"<","a.txt";
my ($row,$col,$val)=(4,1,'haha');
while(<$fh>){
	if($.==$row){
		my @arr=split;
		$arr[$col-1]=$val;
		print join " ",@arr;
		print "\n";
	}
	else{
		print;
	}
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Open and edit a file using a shell script

Hello Folks, I have a file named as date.dat present at /tmp/abc location which has following data - 20161030,20161031,20161101 I need to remove this line and replace it with something like below - $param1,$param2,$param3 Param1, Param2 and param3 stores the date based on some calculation in... (1 Reply)
Discussion started by: ektubbe
1 Replies

2. UNIX for Dummies Questions & Answers

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. nawk -v r=2 -v c=3 -v val=5 -F,... (7 Replies)
Discussion started by: jimmyf
7 Replies

3. Homework & Coursework Questions

Edit the file in shell script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to automate hadoop installation procedure using shell script. It involves go to perticular directory... (3 Replies)
Discussion started by: Abdul Navaz
3 Replies

4. Shell Programming and Scripting

How to create multiline csv cell through shell script?

Hi, I have a text like the one given below status="Observation 1" read1="Source rows not load" read2="Score drop" I want to create a csv and mail it out in such a way that all three lines will be in a single cell but as three lines. For ex Col C1 ... (3 Replies)
Discussion started by: prasperl
3 Replies

5. Shell Programming and Scripting

shell script to edit a file

i have a file called number which contains data as 1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 needed a shell script to print the output as 1 7 7 1 4 and (2 Replies)
Discussion started by: jacky29
2 Replies

6. 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

7. Shell Programming and Scripting

Shell script to edit a file

Hello, I have a big file in wich I would like to rename inside this exactly the string '_ME' and not rename in case we have 'ABC_MELANGE'. Is there a way to do it by using a shell script? Any tip will be apreciated. The file is like described bellow, after using command more filename : ... (3 Replies)
Discussion started by: Titas
3 Replies

8. 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

9. Shell Programming and Scripting

Edit a config file using shell script

I need to edit a config file using shell script. i.e., Search with the 'key' string and edit the 'value'. For eg: below is what I have in the config file "configfile.cfg". Key1=OldValue1 Key2=OldValue2 I want to search for "Key1" and change "OldValue1" to "NewValue1" Thanks for your... (7 Replies)
Discussion started by: rajeshomallur
7 Replies

10. Shell Programming and Scripting

shell script to edit the content of a file

Hi I need some help using shell script to edit a file. My original file has the following format: /txt/email/myemail.txt /txt/email/myemail2.txt /pdf/email/myemail.pdf /pdf/email/myemail2.pdf /doc/email/myemail.doc /doc/email/myemail2.doc I need to read each line. If the path is... (3 Replies)
Discussion started by: tiger99
3 Replies
Login or Register to Ask a Question