Need script to convert TXT file into CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need script to convert TXT file into CSV
# 1  
Old 04-25-2015
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

Code:
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



Code:
              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...
Code:
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

Moderator's Comments:
Mod Comment Use CODE tags for sample input, output, AND code segments.

Last edited by Don Cragun; 04-25-2015 at 03:16 PM.. Reason: Add CODE tags, change Bold tags to CODE tags, & change ICODE tags to CODE tags.
# 2  
Old 04-25-2015
Do you have GNU awk?

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

Or this should work on any version:
Code:
awk '/NOD/ {print $2 OFS substr($3,2,length($3)-2) OFS substr($6,2,length($6)-2)}' Output.TXT

This User Gave Thanks to CarloM For This Post:
# 3  
Old 04-25-2015
Try also
Code:
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

This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-25-2015
its working,...
But i want csv in this format...NODE/ID/IP column


Code:
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 ----------

Image

Last edited by Don Cragun; 04-25-2015 at 03:20 PM.. Reason: Change CENTER tags to CODE tags.
# 5  
Old 04-25-2015
How about trying sth yourself? Anyhow, try
Code:
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
.
.
.

This User Gave Thanks to RudiC For This Post:
# 6  
Old 04-25-2015
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.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 04-25-2015
Do you mean you want the output in fixed-width columns?
This User Gave Thanks to CarloM For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert a txt file to a CSV file

Hi , I have a Txt file which consist of 1000's of SOAP request and response and i want the file to be converted to a csv file like column a should have a soap request and column b should have the soap response . can someone assist me in achieving this please ? Thanks (2 Replies)
Discussion started by: kumarm8
2 Replies

2. Shell Programming and Scripting

Convert shell script output txt file to html table

My concnern related to the post -Convert shell script output txt file to html table, in this how to print the heading as color. awk 'BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}' <filename> (8 Replies)
Discussion started by: sarajobmai
8 Replies

3. Shell Programming and Scripting

Script to convert CSV file to HTML

Hi, I have made a a script which creates a csv file as daily database report However i want to covert that csv file to html because csv file does not have a good visibilty. So it is possible to have such csv to html coversion script. Your prompt help much appreciated. Thanks in advance (4 Replies)
Discussion started by: sv0081493
4 Replies

4. UNIX for Dummies Questions & Answers

Help with a project. convert a txt to csv

Hi people. I've finally converted to linux, and I'm starting to explore the amazing capabilities of the terminal. At the moment in trying to learn how to extract text using the "grep" and "sed" command. I decided to learn by trying to figure out how to solve a practical problem. I have a schedule... (4 Replies)
Discussion started by: kugalskaper
4 Replies

5. Shell Programming and Scripting

Convert shell script output txt file to html table

Hi, I have script which generates the output as below: Jobname Date Time Status abc 12/9/11 17:00 Completed xyz 13/9/11 21:00 Running I have the output as a text file. I need to convert it into a HTML Table and sent it thru email ... (6 Replies)
Discussion started by: a12ka4
6 Replies

6. Shell Programming and Scripting

Convert txt to csv

Hi - I am looking to convert the following text to csv. The columns may not always have data in them and they may have varying spaces but I still need to have a comma there anyway: Sample Data: ~~~~~~~ Name Email Location Phone Tom... (4 Replies)
Discussion started by: JPBovaird
4 Replies

7. Programming

awk script to convert a text file into csv format

hi...... thanks for allowing me to start a discussion i am collecting usb usage details of all users and convert it into csv files so that i can export it into some database.. the input text file is as follows:- USB History Dump by nabiy (c)2008 (1) --- Kingston DataTraveler 130 USB... (2 Replies)
Discussion started by: certteam
2 Replies

8. Shell Programming and Scripting

Is there any script which convert binary file to CSV format

Dear guys; I have a binary file and I need to convert its data to csv format ...appreciating your help. Best Regards (14 Replies)
Discussion started by: ahmad.diab
14 Replies

9. Shell Programming and Scripting

Txt to csv convert

Hi, I was trying some split command to pull out values like "uid=abc,ou=INTERNAL,ou=PEOPLE" into a csv file. However because of erratic nature of occurrance of rows made me stopped. Could someone help me in this? and if someone has a one liner for this? The text file contain pattern like this... (14 Replies)
Discussion started by: john_prince
14 Replies

10. Shell Programming and Scripting

how to convert XLS to CSV and DOC/RTF to TXT

Hi, i don't know anything about PERL. Can anyone help me providing PERL scripts for 1. converting XLS to CSV (and vice-versa) 2. converting DOC/RTF to TXT Thanks much Prvn (1 Reply)
Discussion started by: prvnrk
1 Replies
Login or Register to Ask a Question