0

i am trying to insert blank columns as pre defined location which will have only the headers ( with no values) and then replace the orginal csv with the new one. In nutshell my input csv file will be like (which do not have fix number of rows) :

cat sample.csv

CI Ref,SerialNumber,Last Report Time
VMware-42 2b cb d6 e7 36 ec 73-f4 77 bd 2b f2 6f 25 5d,VMware-42 2b cb d6 e7 36 ec 73-f4 77 bd 2b f2 6f 25 5d,"Fri, 30 May 2014 12:23:13 +0000"
VMware-42 2b 79 e0 da 18 2e 9e-17 b2 6e e1 67 df 3c 6b,VMware-42 2b 79 e0 da 18 2e 9e-17 b2 6e e1 67 df 3c 6b,"Fri, 30 May 2014 12:26:08 +0000"
VMware-42 2b e7 9e 5e c6 48 b6-25 36 6e 1e da 6c 6d d2,VMware-42 2b e7 9e 5e c6 48 b6-25 36 6e 1e da 6c 6d d2,"Fri, 30 May 2014 12:39:54 +0000"
VMware-42 2b b0 e5 dc 56 bd 89-f2 87 de 0b 17 11 43 6e,VMware-42 2b b0 e5 dc 56 bd 89-f2 87 de 0b 17 11 43 6e,"Fri, 30 May 2014 12:39:58 +0000"
VMware-42 2b e6 f0 6a 81 a5 f8-58 f3 9b 07 71 31 10 f8,VMware-42 2b e6 f0 6a 81 a5 f8-58 f3 9b 07 71 31 10 f8,"Fri, 30 May 2014 12:46:30 +0000"
VMWARE-42_2B_6A_35_14_1E_87_54-35_75_8E_2E_33_66_94_44,VMWARE-42_2B_6A_35_14_1E_87_54-35_75_8E_2E_33_66_94_44,"Fri, 30 May 2014 12:49:21 +0000"
VMware-42 2b d9 2d de 16 64 96-43 c3 f8 4a c2 18 c7 8b,VMware-42 2b d9 2d de 16 64 96-43 c3 f8 4a c2 18 c7 8b,"Fri, 30 May 2014 12:51:46 +0000"
VMware-42 2b dd 58 9f 55 f0 c5-04 06 3a a4 26 dd cc 3c,VMware-42 2b dd 58 9f 55 f0 c5-04 06 3a a4 26 dd cc 3c,"Fri, 30 May 2014 12:52:45 +0000"
VMware-42 2b e5 49 0f ac 9c a7-23 bd aa 06 b2 3c df 41,VMware-42 2b e5 49 0f ac 9c a7-23 bd aa 06 b2 3c df 41,"Fri, 30 May 2014 12:52:50 +0000"

i am trying this csv to be converted in below fashion (that is inserting blank columns(only with headers):

CI Name,CI Description,CI Ref,SerialNumber,Last Report Time,abc
<blank>,<blank>,VMware-42 2b cb d6 e7 36 ec 73-f4 77 bd 2b f2 6f 25 5d,VMware-42 2b cb d6 e7 36 ec 73-f4 77 bd 2b f2 6f 25 5d,"Fri, 30 May 2014 12:23:13 +0000",<blank>
<blank>,<blank>,VMware-42 2b 79 e0 da 18 2e 9e-17 b2 6e e1 67 df 3c 6b,VMware-42 2b 79 e0 da 18 2e 9e-17 b2 6e e1 67 df 3c 6b,"Fri, 30 May 2014 12:26:08 +0000",<blank>

3 Answers 3

1

Referred from here

You can use awk or sed tools. 1. To insert a new column (say serial number) before the 1st column

$ awk -F, '{$1=++i FS $1;}1' OFS=, file
1,Unix,10,A
2,Linux,30,B
3,Solaris,40,C
4,Fedora,20,D
5,Ubuntu,50,E

$1=++i FS $1 => Space is used to concatenate columns in awk. This expression concatenates a new field(++i) with the 1st field along with the delimiter(FS), and assigns it back to the 1st field($1). FS contains the file delimiter.

Hope you can work your way from here.

Sign up to request clarification or add additional context in comments.

Comments

1

Ordinarily it would be best to use the Text::CSV module to process CSV data, but since we are adding just empty fields there is no need to parse any data - the additional columns can just be added to the text already there.

Something like this will do what you ask

use strict;
use warnings;
use autodie;

open my $fh, '<', 'data.csv';

chomp(my $head = <$fh>);
print "CI Name,CI Description,$head,abc\n";

while (<$fh>) {
  chomp;
  print ",,$_,\n";
}

output

CI Name,CI Description,CI Ref,SerialNumber,Last Report Time,abc
,,VMware-42 2b cb d6 e7 36 ec 73-f4 77 bd 2b f2 6f 25 5d,VMware-42 2b cb d6 e7 36 ec 73-f4 77 bd 2b f2 6f 25 5d,"Fri, 30 May 2014 12:23:13 +0000",
,,VMware-42 2b 79 e0 da 18 2e 9e-17 b2 6e e1 67 df 3c 6b,VMware-42 2b 79 e0 da 18 2e 9e-17 b2 6e e1 67 df 3c 6b,"Fri, 30 May 2014 12:26:08 +0000",
,,VMware-42 2b e7 9e 5e c6 48 b6-25 36 6e 1e da 6c 6d d2,VMware-42 2b e7 9e 5e c6 48 b6-25 36 6e 1e da 6c 6d d2,"Fri, 30 May 2014 12:39:54 +0000",
,,VMware-42 2b b0 e5 dc 56 bd 89-f2 87 de 0b 17 11 43 6e,VMware-42 2b b0 e5 dc 56 bd 89-f2 87 de 0b 17 11 43 6e,"Fri, 30 May 2014 12:39:58 +0000",
,,VMware-42 2b e6 f0 6a 81 a5 f8-58 f3 9b 07 71 31 10 f8,VMware-42 2b e6 f0 6a 81 a5 f8-58 f3 9b 07 71 31 10 f8,"Fri, 30 May 2014 12:46:30 +0000",
,,VMWARE-42_2B_6A_35_14_1E_87_54-35_75_8E_2E_33_66_94_44,VMWARE-42_2B_6A_35_14_1E_87_54-35_75_8E_2E_33_66_94_44,"Fri, 30 May 2014 12:49:21 +0000",
,,VMware-42 2b d9 2d de 16 64 96-43 c3 f8 4a c2 18 c7 8b,VMware-42 2b d9 2d de 16 64 96-43 c3 f8 4a c2 18 c7 8b,"Fri, 30 May 2014 12:51:46 +0000",
,,VMware-42 2b dd 58 9f 55 f0 c5-04 06 3a a4 26 dd cc 3c,VMware-42 2b dd 58 9f 55 f0 c5-04 06 3a a4 26 dd cc 3c,"Fri, 30 May 2014 12:52:45 +0000",
,,VMware-42 2b e5 49 0f ac 9c a7-23 bd aa 06 b2 3c df 41,VMware-42 2b e5 49 0f ac 9c a7-23 bd aa 06 b2 3c df 41,"Fri, 30 May 2014 12:52:50 +0000",

2 Comments

@sErVerdevIL thanks but i am not sure how i can use this [email protected] a lot for help. can you let me know what if i want to add column with header at any random locations , mean to say what if i want to add say 3 columns with thae abc , def , hij in between CI Ref and SerialNumber? also it will be great if you can explain the script to me in brief as i am very mew to the scripting world..
@sErVerdevIL and .@Borodin :i have tried the below way but since one of my input field value contains "," in it which is actually used as a separator in csv , i am getting incorrect output for that particular field :#awk 'BEGIN {FS=",";OFS=","; print "CI Ref,abc,SerialNumber,efg,Last Report Time"} NR >1 {print $1,","$2,","$3}' sample.csv.......output:CI Ref,abc,SerialNumber,efg,Last Report Time VMware-42 2b cb d6 e7 36 ec 73-f4 77 bd 2b f2 6f 25 5d,,VMware-42 2b cb d6 e7 36 ec 73-f4 77 bd 2b f2 6f 25 5d,,"Fri
0
awk -v OFS=, '
    NR==1 {print "CI Name","CI Description",$0; next} 
    {print "","",$0}
' sample.csv

2 Comments

:thanks a lot for your help but can you please advise how can i turn your script into the one which can allow me to even put blank columns in between CI Ref and SerialNumber with some columns header names say Ipaddress and Subnetmask which will have blank values.basically i am looking for a script which can allow me to insert blank columns with headers anywhere in the csv file...thanks in advance
If you have additional requirements, update your question or ask a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.