Visit Our UNIX and Linux User Community

Break Column nth in a CSV file into two

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break Column nth in a CSV file into two
# 1  
Old 09-06-2013
Break Column nth in a CSV file into two

Hi Guys,

Need help with logic to break Column nth in a CSV file into two

for e.g

Refer below the second column as the nth column


need output in a following format



any help is greatly appreciated.
thanks in advance
# 2  
Old 09-06-2013
Hope this helps you

awk -F, '{ for(i=1;i<=NF;i++){
    if($i ~ /^\"/ && $i !~ /\"$/){$i=$i"\""}
    if($i ~ /\"$/ && $i !~ /^\"/){$i="\""$i}S=S?S FS $i : $i}
    print S;S=""}' file

# 3  
Old 09-06-2013
thank a ton for such a quick reply

its definitely working for the above e.g ,wanted to implement this for large dataset and restrict this parsing to apply on particular column, say column 16.

can you please drop a hint.

thanks in advance
# 4  
Old 09-06-2013
for 16th column only
awk -F, '{if($16 ~ /^\"/ && $16 !~ /\"$/){$16=$16"\""}
    if($16 ~ /\"$/ && $16 !~ /^\"/){$16="\""$16}
    gsub(/[][]/,"",$16)}1' file

Last edited by pamu; 09-10-2013 at 07:39 AM.. Reason: Corrected
# 5  
Old 09-10-2013
for some reason this is not working..
# 6  
Old 09-10-2013
Originally Posted by awk-admirer
for some reason this is not working..
Corrected my previous post. Please check.

Previous Thread | Next Thread
Test Your Knowledge in Computers #222
Difficulty: Easy
NetMarketShare reported that the combined versions of Microsoft Windows lead the worldwide market with close to a 93% market share at the end of August 2011, followed by OS X at 6%, and Linux with 1%.
True or False?

9 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

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

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

5. Shell Programming and Scripting

How to remove mth and nth column from a file?

Hi, i need to remove mth and nth column from a csv file. here m and n is not a specific number. it is a variable ex. m=2 n=5 now i need to remove the 2nd and 5th line.. Please help how to do that. Thanks!!! (18 Replies)
Discussion started by: zaq1xsw2
18 Replies

6. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

7. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

8. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

9. Shell Programming and Scripting

How to remove line break in a csv file

Hi Experts, My requirement is to read the csv file and need to remove if any line break in it. sample data: Row1: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 Row2:"Tønsberg, Brygga Kino SF",Tønsberg,202-1, Tønsberg SF 4,202-1-4 Expected data: Row1: "Oslo, Symra... (6 Replies)
Discussion started by: cnraja
6 Replies

Featured Tech Videos