Need script to convert TXT file into CSV

Hi Team,
i have some script which give output in TXT format , need script to convert TXT file into CSV.

Output.TXT

413. U-UU-LVDT-NOD-6002 [NOD_4406]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.494 (1234:333:aaa:2333:3:2:333:a)       
414. U-UU-LVDT-NOD-6004 [NOD_3805]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.589 (1234:333:aaa:2333:3:2:333:e)       
415. U-UU-LVDT-NOD-6005 [NOD_4288]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.349 (1234:333:aaa:1400:3:2:333:1e)      
416. U-UU-LVDT-NOD-6006 [NOD_2810]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.352 (1234:333:aaa:a00:3:2:333:7a)       
417. U-UU-LVDT-NOD-6008 [NOD_2803]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.613 (1234:333:aaa:a00:3:2:333:66)       
418. U-UU-LVDT-NOD-9006 [NOD_2320]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.618 (1234:333:aaa:1400:3:2:333:22)      
419. U-UU-LVDT-NOD-9014 [NOD_2095]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.347 (1234:333:aaa:a00:3:2:333:d6)   

Need to convert it into CSV like this

              NODE	           ID 	                IP
U-UU-LVDT-NOD-6002	 4406	  1234:333:aaa:2333:3:2:333:a
U-UU-LVDT-NOD-6004	 3805	  1234:333:aaa:2333:3:2:333:e
U-UU-LVDT-NOD-6005	 4288	  1234:333:aaa:1400:3:2:333:1e
U-UU-LVDT-NOD-6006	 2810   1234:333:aaa:a00:3:2:333:7a

I try some script like...

grep -F "NOD" /home/lotus/bin/Output.TXT | cut -d ' ' -f3 | cut -c7- | cut -d '' -f1 | sed 's/\(.*\)...../\1/' > /home/lotus/bin/p1.csv
grep -F "NOD" /home/lotus/bin/Output.TXT | cut -d ' ' -f2 | cut -c6- | cut -d '' -f1 | sed 's/\(.*\)..../\1/' >> /home/lotus/bin/p1.csv

it is not giving proper CSV file output

Do you have GNU awk?

Try:

awk '/NOD/ {print $2 OFS $4 OFS $11}' FS=' |\\]|\\[|\\)|\\(' Output.TXT

Or this should work on any version:

awk '/NOD/ {print $2 OFS substr($3,2,length($3)-2) OFS substr($6,2,length($6)-2)}' Output.TXT
1 Like

Try also

awk '{gsub(/\(|\)|\[|\]|NOD_/,_); print $2, $3, $6}' file
U-UU-LVDT-NOD-6002 4406 1234:333:aaa:2333:3:2:333:a
U-UU-LVDT-NOD-6004 3805 1234:333:aaa:2333:3:2:333:e
U-UU-LVDT-NOD-6005 4288 1234:333:aaa:1400:3:2:333:1e
U-UU-LVDT-NOD-6006 2810 1234:333:aaa:a00:3:2:333:7a
U-UU-LVDT-NOD-6008 2803 1234:333:aaa:a00:3:2:333:66
U-UU-LVDT-NOD-9006 2320 1234:333:aaa:1400:3:2:333:22
U-UU-LVDT-NOD-9014 2095 1234:333:aaa:a00:3:2:333:d6
1 Like

its working,...
But i want csv in this format...NODE/ID/IP column

NODE            	    ID	                               IP
U-UU-LVDT-NOD-6002	          4406	 1234:333:aaa:2333:3:2:333:a
U-UU-LVDT-NOD-6004	          3805	 1234:333:aaa:2333:3:2:333:e
U-UU-LVDT-NOD-6005	          4288	 1234:333:aaa:1400:3:2:333:1e
U-UU-LVDT-NOD-6006	          2810	 1234:333:aaa:a00:3:2:333:7a

---------- Post updated at 08:00 AM ---------- Previous update was at 07:55 AM ----------

How about trying sth yourself? Anyhow, try

awk 'BEGIN {print "NODE ID IP"} {gsub(/\(|\)|\[|\]|NOD_/,_); print $2, $3, $6}' file
NODE ID IP
U-UU-LVDT-NOD-6002 4406 1234:333:aaa:2333:3:2:333:a
.
.
.
1 Like

When you don't use CODE tags to show how your input is formatted, it confuses volunteers trying to help you. When you say you want CSV output and show us text that is not in the format of a CSV file in your various sample outputs, it confuses volunteers trying to help you. When you show us an image of what appears to be a screenshot of a spreadsheet program display, it confuses volunteers trying to help you.

1 Like

Do you mean you want the output in fixed-width columns?

1 Like

yes!!

Fixed width definitely is NOT csv (comma separated values). Please make up your mind what exactly you want/need.

As has been stated many times, your requirement for a CSV output file and your requirement for fixed width output fields are conflicting requirements. The fact that you have presented three different versions of your desired output (and none of them match any of your descriptions of your desired output) adds to the confusion.

Furthermore, you have shown us seven lines of input and four lines of output (not counting the added header line) with no indication of why some input lines are supposed to be removed from the output. You say you want fixed width fields, but the headers in two or your three output samples do not have the headers lined up with the corresponding field data. Your sample input seems to have constant width data for the 1st two output fields, and variable width data for the last output field, but there is no indication whether the input will be fixed length for those two fields or if that just happens to be true for the seven sample lines provided.

Some of your sample output uses a tab and 2 spaces as an output field separator, some uses just a tab, some uses a tab and 10 spaces, some uses a tab and 1 space, one uses a tab and 16 spaces, and one uses a tab and 12 spaces.

The following awk script produces fixed width output for the 1st two columns making the 1st output field 8 characters wider than the widest corresponding input field, the 2nd output field 6 characters wider than the widest corresponding input field, and a variable length final field with no padding added to the corresponding input field. (This seems to match some of the lines specified in your first sample output.) The headers are lined up with the underlying field data.

awk -F'[]_ ()]+' '
BEGIN {	h[1] = "NODE";	fw[1] = 4
	h[2] = "ID";	fw[2] = 2
	h[3] = "IP"
}
{	d[NR, 1] = $2
	if(length($2) > fw[1]) fw[1] = length($2)
	d[NR, 2] = $4
	if(length($4) > fw[2]) fw[2] = length($4)
	d[NR, 3] = $(NF - 1)
}
END {	fw[1] += 8
	fw[2] += 6
	printf("%-*s%-*s%s\n", fw[1], h[1], fw[2], h[2], h[3])
	for(i = 1; i <= NR; i++)
		printf("%-*s%-*s%s\n", fw[1], d[i, 1], fw[2], d[i, 2], d[i, 3])
}' Output.TXT

with the sample Output.TXT contents you provided in post #1 in this thread, produces the output:

NODE                      ID        IP
U-UU-LVDT-NOD-6002        4406      1234:333:aaa:2333:3:2:333:a
U-UU-LVDT-NOD-6004        3805      1234:333:aaa:2333:3:2:333:e
U-UU-LVDT-NOD-6005        4288      1234:333:aaa:1400:3:2:333:1e
U-UU-LVDT-NOD-6006        2810      1234:333:aaa:a00:3:2:333:7a
U-UU-LVDT-NOD-6008        2803      1234:333:aaa:a00:3:2:333:66
U-UU-LVDT-NOD-9006        2320      1234:333:aaa:1400:3:2:333:22
U-UU-LVDT-NOD-9014        2095      1234:333:aaa:a00:3:2:333:d6

and with the input:

413. U-UU-LVDT-NOD-6002 [NOD_4406]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.494 (1234:333:aaa:2333:3:2:333:a)       
414. U-UU-LVDT-NOD-6004 [NOD_3805]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.589 (1234:333:aaa:2333:3:2:333:e)       
415. U-UU-LVDT-NOD-6005 [NOD_4288]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.349 (1234:333:aaa:1400:3:2:333:1e)      
416. U-UU-LVDT-NOD-6006 [NOD_2810]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.352 (1234:333:aaa:a00:3:2:333:7a)       
417. U-UU-LVDT-NOD-6008 [NOD_2803]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.613 (1234:333:aaa:a00:3:2:333:66)       
418. U-UU-LVDT-NOD-9006 [NOD_2320]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.618 (1234:333:aaa:1400:3:2:333:22)      
419. U-UU-LVDT-NOD-9014 [NOD_2095]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.347 (1234:333:aaa:a00:3:2:333:d6)
420. U-UU-LVDT-NOD-9014-MOD2 [NOD_2] macro_outcome_dist-8.0.0(v1_0_2)   KK:1.2.347 (1234:333:aaa:a00:333:222:333:1d6)
421. U-UU-LVDT-NOD-9014-MODEL2015 [NOD_1234567890]           macro_outcome_dist-8.0.0(v1_0_2)    KK:1.2.347 (1:3:a:a0:3:2:3:6)

produces the output:

NODE                                ID              IP
U-UU-LVDT-NOD-6002                  4406            1234:333:aaa:2333:3:2:333:a
U-UU-LVDT-NOD-6004                  3805            1234:333:aaa:2333:3:2:333:e
U-UU-LVDT-NOD-6005                  4288            1234:333:aaa:1400:3:2:333:1e
U-UU-LVDT-NOD-6006                  2810            1234:333:aaa:a00:3:2:333:7a
U-UU-LVDT-NOD-6008                  2803            1234:333:aaa:a00:3:2:333:66
U-UU-LVDT-NOD-9006                  2320            1234:333:aaa:1400:3:2:333:22
U-UU-LVDT-NOD-9014                  2095            1234:333:aaa:a00:3:2:333:d6
U-UU-LVDT-NOD-9014-MOD2             2               1234:333:aaa:a00:333:222:333:1d6
U-UU-LVDT-NOD-9014-MODEL2015        1234567890      1:3:a:a0:3:2:3:6

If you want to try this script on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk .

Hopefully, this is close to what you want. If not, you MUST give us a clear specification of your input file format and a clear specification of your desired output file format or we are all wasting our time trying to help you.

1 Like

Really sorry for adding confusion.....
I need fixed width xls format.

---------- Post updated 04-27-15 at 05:46 AM ---------- Previous update was 04-26-15 at 11:45 PM ----------

HI team,
getting output logs wrong in different format from telnet script ...

Output.txt

[33m19.[0m [34mU-UU-LVDT-NODE-6006[0m [[33mNODE_3233[0m]            macro_outdoor_dist-6.0.0(v4_0_2)                DN:1.3.903 (1101:100:11w:500:3:2:103:aa)       
[33m20.[0m [31mU-UU-LVDT-NOD-6009[0m [[33mNOD_3163[0m]            macro_outdoor_dist-8.1.0(v3_1_0)                DN:1.3.409 (N/A)       
[33m21.[0m [31mU-UU-LVDT-NOD-6010[0m [[33mNOD_3167[0m]            macro_outdoor_dist-7.1.0(v3_1_0)                DN:1.3.424 (N/A)       
[33m22.[0m [31mU-UU-LVDT-NOD-6011[0m [[33mNOD_4454[0m]            macro_outdoor_dist-6.0.0(v4_0_2)                DN:1.3.398 (1101:100:11e:300:3:2:103:dd)       
[33m23.[0m [31mU-UU-LVDT-NOD-6012[0m [[33mNOD_3180[0m]            macro_outdoor_dist-6.1.0(v3_1_0)                DN:1.3.420 (N/A)       
[33m24.[0m [31mU-UU-LVDT-NOD-9011[0m [[33mNOD_2132[0m]            macro_outdoor_dist-6.1.0(v3_1_0)                DN:1.3.388 (N/A)       
[33m25.[0m [31mU-UU-LVDT-NOD-9013[0m [[33mNOD_2114[0m]            macro_outdoor_dist-6.1.0(v3_1_0)                DN:1.3.389 (N/A)       
[33m26.[0m [34mU-UU-LVDT-NOD-6000[0m [[33mNOD_2891[0m]            macro_outdoor_dist-6.0.0(v4_0_2)                DN:1.3.724 (1101:100:11a:300:3:2:103:aa)  

When run command manually its give right output like this but by using script output come diffrent
Right Output.txt is

Eg...

413. U-UU-LVDT-NOD-6002 [NOD_4406]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.494 (1234:333:aaa:2333:3:2:333:a)       
414. U-UU-LVDT-NOD-6004 [NOD_3805]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.589 (1234:333:aaa:2333:3:2:333:e)       
415. U-UU-LVDT-NOD-6005 [NOD_4288]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.349 (1234:333:aaa:1400:3:2:333:1e)      
416. U-UU-LVDT-NOD-6006 [NOD_2810]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.352 (1234:333:aaa:a00:3:2:333:7a)       
417. U-UU-LVDT-NOD-6008 [NOD_2803]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.613 (1234:333:aaa:a00:3:2:333:66)       
418. U-UU-LVDT-NOD-9006 [NOD_2320]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.618 (1234:333:aaa:1400:3:2:333:22)      
419. U-UU-LVDT-NOD-9014 [NOD_2095]           macro_outcome_dist-8.0.0(v1_0_2)                KK:1.2.347 (1234:333:aaa:a00:3:2:333:d6)