Sponsored Content
Top Forums Shell Programming and Scripting Inserting blank columns in already present CSV file Post 302903892 by Akshay Hegde on Friday 30th of May 2014 10:52:19 AM
Old 05-30-2014
Hope this might make your life easy, sorry for late reply

Code:
$ cat file
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"

Code:
$ cat addheader
#!/bin/bash

cat >header<<EOF
before=1
CI Name=<blank>
CI Description=<blank>

after=3
abc=<blank>
EOF

awk '

BEGIN{
	file="header"; OFS=","

	while(getline line <file)
	{
		split(line,Arr,/=/)

		if(tolower(Arr[1])=="before")
		{
			key = "b"  
			n = Arr[2] 
			continue
		}
		else if(tolower(Arr[1])=="after")
		{
			key = "a" 
			n = Arr[2] 
			continue
		}	
		
		
		if(length(line))
		{
			H[n FS key FS 1] = ((n FS key FS 1) in H) ? H[n FS key FS 1] OFS Arr[1] : Arr[1]
			H[n FS key FS 0] = ((n FS key FS 0) in H) ? H[n FS key FS 0] OFS Arr[2] : Arr[2]
		}
	}
	close(file)


    }

    {
	c=0 ; str = ""
	$0 = $0","                                   
	while($0) {
  			match($0,/ *"[^"]*" *,|[^,]*,/) 
  			f=substr($0,RSTART,RLENGTH)             
  			gsub(/,$/,"",f)               
			c++  		
		
			if(length(f))
			{
			ind = FNR==1 ? FNR : 0			
			
			f = ((c FS "a" FS ind) in H) ? \
			    f OFS H[c FS "a" FS ind] : f

			f = ((c FS "b" FS ind) in H) ? \
			    H[c FS "b" FS ind] OFS f : f
			    
			str = str == "" ? f : str OFS f
			}
			
  			$0 = substr($0,RLENGTH+1)                
		  }

		if(length(str))print str
    }
   ' file 

Resulting
Code:
$ bash addheader
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>

use nawk if you are using sunos/solaris

Header format

Code:
<before|after>=field number
header_name1 = value1
header_name2 = value2

if no headername and no value only equal = it just creates empty fields..

do experiments...

Last edited by Akshay Hegde; 05-30-2014 at 01:31 PM.. Reason: to add header format
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting 2 columns from a file to another with nawk

Hello all, I have these 2 files File1 123 100 456 200 789 300 File2 |1|2|3||4|5||6| |1|2|3||4|5||6| |1|2|3||4|5||6| I need an output like : |1|2|3|123|4|5|100|6| |1|2|3|456|4|5|200|6| |1|2|3|789|4|5|300|6| (3 Replies)
Discussion started by: rany1
3 Replies

2. Shell Programming and Scripting

How to remove Blank rows in a csv file

Hi, I need help to remove blank rows at the end of file. Sample data: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 "Tønsberg, Brygga Kino SF",Tønsberg,202-1,Tønsberg SF 4,202-1-4 ,,,, ,,,, ,,,, ,,,, Expected data: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 "Tønsberg, Brygga... (6 Replies)
Discussion started by: cnraja
6 Replies

3. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

4. Shell Programming and Scripting

Inserting additional comma delimiters in a csv file, after and before certian fields.

Hello I have a csv file which I need to insert addtional commas into. The csv is of the format field1,field2,field3,field4,...etc...,field13,field14 I need to add extra commas in each record so that the final output looks like ... (1 Reply)
Discussion started by: kamal_p_99
1 Replies

5. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

Inserting new fields to a csv file

hi I have a csv file with few rows > cat job_stat 1,jobname1,somthing,somthing 2,jobname2,somthing,somthing 3,jobname3,somthing,somthing 4,jobname4,somthing,somthing I want to add few columns after the 2nd column and then append rest of the columns after the 3rd newly added... (3 Replies)
Discussion started by: midhun19
3 Replies

7. Shell Programming and Scripting

Randomly inserting extra columns into csv file

Hi Tech Guru, I have a test file as below , which needs some more fields to be populated randomly : dks3243;12;20130823;1420;25m;0;syt dks3243;rocy;10 dks3243;kiop;18 sde21p4;77;20151210;8479;7py;9;vfr sde21p4;temp;67 sfq6i01;12;20120123;3412;4rd;7;jui sfq6i01;uymk;90 sfq6i01;kiop;51 ... (8 Replies)
Discussion started by: Lokesha
8 Replies

8. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

9. Shell Programming and Scripting

Detect blank spaces in a CSV file

I'm monitoring a WLAN network to keep track of new SSIDs popping up. The SSIDs are stored along with the AP MAC address and a few other parameters in a CSV file. A typical line could look like this: 18:70:9f:e3:80:aa 10:11:15 MyNetwork 2437 Now, the problem is that some networks use SSIDs... (4 Replies)
Discussion started by: Zooma
4 Replies

10. UNIX for Beginners Questions & Answers

Inserting a blank line at the end of a .txt file?

Hi there, I am having this problem: a) I am uploading a txt file from windows (notepad) with some Gaussian 09 command lines; b) Gaussian needs in certain command files, that those files have a blank line at the end of the file! c) I open the command file with vi and no blank line at the of... (2 Replies)
Discussion started by: luismga
2 Replies
All times are GMT -4. The time now is 09:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy