Replace 2nd column of CSV file with numbers on line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace 2nd column of CSV file with numbers on line
# 1  
Old 09-04-2011
Tools Replace 2nd column of CSV file with numbers on line

I have a csv file with occasional multiple entries in the second column.

Code:
111111,104,07-24-2011,3.15,N,
222222,020 140,07-24-2011,10.00,N,

I want the result

Code:
111111,104,07-24-2011,3.15,N,
222222,020,07-24-2011,10.00,N,
222222,140,07-24-2011,10.00,N,

I know I can get the output of the second column with awk but I am having trouble with bin/bash using regex to replace the remainer of the lines correctly

Code:
awk -F "," '{print $2}' Temp.csv


Last edited by ffdstanley; 09-04-2011 at 02:38 PM.. Reason: Code tags!
# 2  
Old 09-04-2011
Try:
Code:
awk -F"," -vOFS="," '$2~" "{n=split ($2,a," ");for (i=1;i<=n;i++){$2=a[i];print};next}1' Temp.csv

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 09-04-2011
Ops, first answer was wrong Smilie
Code:
awk 'BEGIN{FS=OFS=","}{x=split($2,a," ");for(i=0;++i<=x;){$2=a[i];print}}'  file

This User Gave Thanks to danmero For This Post:
# 4  
Old 09-04-2011
Thanks danmero! I thought it would be tougher than that.... Thanks again!
# 5  
Old 09-04-2011
Quote:
Originally Posted by ffdstanley
Thanks danmero!
bartus11 provided the first correct solution Smilie
# 6  
Old 09-04-2011
Quote:
Originally Posted by danmero
bartus11 provided the first correct solution Smilie
The following part is not portable (no space between the -v option and the identifier's name):

-vOFS=","

That might be the reason why the OP thanked you and not bartus11 Smilie

P.S. Now I see that both solutions got thank you, so I suppose I was wrong.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Shell Programming and Scripting

Use awk to replace numbers in a file with a column from another file

Hello, I am trying to make a awk code that will take 2 files, a txt file like this : 1 1 88 c(1:38, 42, 102) 2 2 128 c(39:41, 43:101, 103:105, 153, 155:189, 292, 344:369) 3 3 84 c(190:249, 603, 606:607, 609:629) 4 4 12 ... (8 Replies)
Discussion started by: nastaziales
8 Replies

3. Shell Programming and Scripting

Column with New Line in CSV file

Hello, Got a CSV file which contains 21 columns Need to convert the file to Pipe delimiter and Few columns text data contains new line Example 1,2,3,"ABC" 8" ABC,5,6,7 1,2,3,"ABC" 8" ABC,5,6,7 ( New Line) 1,2,3,""ABC" 8" ABC", 5,6,7 1,2,3,"ABC" ,5,6,7(New line) Expected... (8 Replies)
Discussion started by: krux_rap
8 Replies

4. Shell Programming and Scripting

CSV file REPLACE COLUMN if it matches

I have a file cat 1.txt AAAA , BBBB , CCCC , DDDD DFDF , DFDF , DFDF , FDDD AA11 , DFDF , 0000 , UTIO ADSD , WERT, 0000 , JKJL If the 3rd column is not equal to "0000" , then it should replace "0000" with "XXXX" and if its equal to "0000" then print the line as it is. need help. (9 Replies)
Discussion started by: aravindj80
9 Replies

5. Shell Programming and Scripting

Replace 2nd column in file

I have following entries file abc.txt abc83.out.remote TRUE abc84.out.remote TRUE abc85.out.remote TRUE abc86.out.remote TRUE Please help me, how do i toggle the entries listed in 2nd column based on the search patterns (abcxx) abcxx, i can get... (14 Replies)
Discussion started by: sdosanjh
14 Replies

6. Shell Programming and Scripting

Split a file into multiple files based on line numbers and first column value

Hi All I have one query,say i have a requirement like the below code should be move to diffent files whose maximum lines can be of 10 lines.Say in the below example,it consist of 14 lines. This should be moved logically using the data in the fisrt coloumn to file1 and file 2.The data of first... (2 Replies)
Discussion started by: sarav.shan
2 Replies

7. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

8. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

9. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, i have a csv file . In the 7th column i have data that has line feed in it. Requirement is to remove the line feed from the 7th column whenever it appears There are 11 columns in the file C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11 The value in C7 contains line feed ( Alt + Enter ),... (2 Replies)
Discussion started by: r_t_1601
2 Replies

10. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, My requirement is to remove line (3 Replies)
Discussion started by: r_t_1601
3 Replies
Login or Register to Ask a Question