How to add a new column in a csv file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add a new column in a csv file?
# 1  
Old 07-23-2013
How to add a new column in a csv file?

Hi All,

I would like to add a new column in my input.csv file as well as also want to add a new page in same .csv file.

Plz help me any help should highly be appreciated...Smilie
# 2  
Old 07-23-2013
Hello,

Could you please give some code example for same? it will be helpful for us.



Thanks,
R. Singh
# 3  
Old 07-23-2013
Quote:
Originally Posted by RavinderSingh13
Hello,

Could you please give some code example for same? it will be helpful for us.



Thanks,
R. Singh
Hi Sir,

I m new in shell so please help me
# 4  
Old 07-23-2013
Hello,

As your requirement is still not clear to me. What I assume is you need to enter a new column to a file. If that is the requirement please try the following code.

lets say there is file named file_name which have columns as follows.

Code:
ID GENE Newcol pos start end


Command is:
Code:
awk 'NR==1 {$2=$2 "\t""Newcol"; print; next}' file_name

Output should be:

Code:
ID GENE Newcol pos start end


So after running the command a newcol is being added. Bot sure if this may help you. Please get back to us in case you need more assistance for same.




Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-23-2013
Quote:
Originally Posted by RavinderSingh13
Hello,

As your requirement is still not clear to me. What I assume is you need to enter a new column to a file. If that is the requirement please try the following code.

lets say there is file named file_name which have columns as follows.

Code:
ID GENE Newcol pos start end


Command is:
Code:
awk 'NR==1 {$2=$2 "\t""Newcol"; print; next}' file_name

Output should be:

Code:
ID GENE Newcol pos start end


So after running the command a newcol is being added. Bot sure if this may help you. Please get back to us in case you need more assistance for same.




Thanks,
R. Singh

thanks a lot it works ...

But I want to push data in a new column on daily basis using unix shell script I want to add a column and push data from text file on daily basis. Is it possible in shell script Please help me
# 6  
Old 07-23-2013
I would suggest posting a representative sample input and desired output in code tags
This User Gave Thanks to Yoda For This Post:
# 7  
Old 07-23-2013
Quote:
Originally Posted by Yoda
I would suggest posting a representative sample input and desired output in code tags
input file: in.csv contains
Code:
description,20-07-2013,21-07-2013,22-07-2013
total IP Failed,500,400,700
Bharti Ip,400,350,500
Non- Bharti IP,100,50,200
static IP,50,10,100
Dyanmic IP,350,340,400
North,100,140,100
South,100,100,100
West,150,100,100

Output file contains today's date as a column name and data of this column is also coming from some commands. Column data is dynamic in nature

Output file looks like-
Code:
description,20-07-2013,21-07-2013,22-07-2013,23-07-2013
total IP Failed,500,400,700,200
Bharti Ip,400,350,500,150
Non- Bharti IP,100,50,200,50
static IP,50,10,100,50
Dyanmic IP,350,340,400,100
North,100,140,100,30
South,100,100,100,30
West,150,100,100,40

Please help....

Last edited by Scott; 07-23-2013 at 01:22 PM.. Reason: Use code tags, not icode tags
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare every column from one csv file to another csv file

1.csv contains following column- Empid code loc port 101 A xy 01 102 B zx 78 103 A cg 12 104 G xy 78 2.csv contains follwing data- Empid code loc port 101 A gf 01 102 B zx 78 103 C cg 32 104 ... (1 Reply)
Discussion started by: rishabh
1 Replies

2. Shell Programming and Scripting

Add current time stamp column in existing csv file

Hi , I want to add a new column 'current_time stamp' in my existing csv file with current time stamp for all the records.I tried something this but this is printing 0 with date & time and printed date one line above header.Please help awk -F "," 'BEGIN{ OFS="," } {$6=system("date... (5 Replies)
Discussion started by: netdbaind
5 Replies

3. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

4. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

5. Shell Programming and Scripting

First 4 Characters of a column in CSV file

Hi All, I have a CSV file in the below format: Id,Text,File "123","This is unix file","Unix" "121","This is C file","C" Want to have output in below format: Id,Text,File "123","This","Unix" "121","This","C" File is , separator with each fields in quotes. Some fields are having... (4 Replies)
Discussion started by: Sufer
4 Replies

6. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

7. Shell Programming and Scripting

Pick the column value based on another column from .csv file

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Main imp point, in my .csv file, third column is having price value with comma (1,20,300), it has... (2 Replies)
Discussion started by: Ganesh L
2 Replies

8. Shell Programming and Scripting

Appending = in particular column in csv file

Hi, I have a requirement to append = in particular row in csv file. Data in csv is as follow: row1,a,a,a row2,b,b,b row3,c,c,c row4,d,d,d csv should be modified at row3 and no. of columns are not fixed but rows are. output should be as: row1,a,a,a row2,b,b,b row3,=c,=c,=c... (2 Replies)
Discussion started by: Divya1987
2 Replies

9. Shell Programming and Scripting

add a additional column in csv file

Thanks for allwoing me to discuss in this forum GIVEN BELOW A simple shell script which will ask for the user to input a PC name and it will produce the output in CSV with the PC name #! /bin/bash read -p "enter the PC name :" pc #checking for netstat report netstat -pant |sed '1,2d'... (1 Reply)
Discussion started by: ayyappancheta
1 Replies
Login or Register to Ask a Question