Generate csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate csv file
# 1  
Old 01-23-2007
Question Generate csv file

I have a file which has some thousand records in the following format

File: input.txt ->


<option value="14333">VISWANADH VELAMURI</option>

<option value="17020">VISWANADHA RAMA KRISHNA</option>


I want to generate a csv file from the above file as follows

File: output.txt ->

14333,VISWANADH VELAMURI
17020,VISWANADHA RAMA KRISHNA


The HTML option tags are to be removed alongwith the unwanted tabs and the empty lines in between. I have tried cut, awk, but I am not getting the correct combination. Can you please help me out, as I want to upload this data into a database table.

Thanks.
# 2  
Old 01-23-2007
If you have Python, here's an alternative:
Code:
import re
for line in open("inputfile"):
     print ','.join(re.findall(r'<.*value=\"(.*)\">(.*)<.*?>',line)[0])

from command line:
Code:
#/home: python script.py > output.csv

# 3  
Old 01-23-2007
With GNU awk/nawk:

Code:
awk '$0=$0{printf "%s,%s\n",$2,$3}' \
FS="<option value=\"|\">|</option>" infile

# 4  
Old 01-23-2007
Code:
#! /opt/third-party/bin/perl

open (FILE, "< $ARGV[0] ") || die "Unable to open $ARGV[0] <$!> \n";

my(@split_fields, @second_split, @further);

while( chomp($_ = <FILE> ) ) {
  @split_fields = split(/"/, $_);
  @second_split = split(/>/, $_);
  @further = split(/</, $second_split[1]);
  print "$split_fields[1],$further[0]\n";
}

close(FILE);

exit 0

# 5  
Old 01-23-2007
one more,
Code:
sed 's/\(.*\)\"\(.*\)\"\(.*\)>\(.*\)<\(.*\)/\2,\4/'  filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate .csv/ xls file report

There can be thousand of .ksh in a specific directory where sql files are called from ksh. Requirement is to loop through all the files content and generate a report like below: Jobname Type type sqlname gemd1970 sql daily tran01 gemw1971 sql weekly ... (6 Replies)
Discussion started by: vedanta
6 Replies

2. Shell Programming and Scripting

Script to generate .csv file

Dears,I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this. The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the file as an input the file will be... (8 Replies)
Discussion started by: abdul2020
8 Replies

3. Shell Programming and Scripting

Script to generate csv file

Dears, I am new in shell world and I need your help in this, I have to create a report based on the output file generated by another program. I want to write a shell script for this. The output file generated every 15 minutes but i can’t open it until the end of day so the script will get the... (3 Replies)
Discussion started by: abdul2020
3 Replies

4. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

5. Shell Programming and Scripting

Script to generate csv file

Hello; I need to generate a csv file that contains a list of all the files in a particular server (from the root directory ie: \) that have a permission stamp of 777. I would like to create the csv so that it contains the following: server name, file name, full path name where file exists,... (17 Replies)
Discussion started by: gvolpini
17 Replies

6. Shell Programming and Scripting

Read a CSV file and generate SQL output

Friends, This is what I need: I will pass a CSV file as an input, and I want my shell to be reading that CSV file, and based on the parameters it should generate SQLs and write those SQL in a different file in the same location. I'm new to Shell scripting. I'm currently working on a... (25 Replies)
Discussion started by: Ram.Math
25 Replies

7. Shell Programming and Scripting

to read a CSV file and generate SQL output

Friends, This is what I need: I will pass a CSV file as an input, and I want my shell to be reading that CSV file, and based on the parameters it should generate SQLs and write those SQL in a different file in the same location. I'm new to Shell scripting. I'm currently working on a... (1 Reply)
Discussion started by: Ram.Math
1 Replies

8. Shell Programming and Scripting

Need to generate .csv file

I have a csv file with the following data Please find the attachment - zip ... (6 Replies)
Discussion started by: vaas
6 Replies

9. Shell Programming and Scripting

need help in Parsing a CSV file and generate a new output file

Hi Scripting Gurus, I am trying to parse a csv file and generate a new output file. The input file will be a variable length in turns of rows and columns. output file will have 8 columns. we have three columns from the header for each set. just to give little bit more clarification each row... (15 Replies)
Discussion started by: vkr
15 Replies

10. UNIX for Dummies Questions & Answers

generate CSV file using AWK script

Hi guys I have a text report that consists of text in some parts and data in some parts. e.g Report for changes in cashflows No changes were found Report for changes in Bills deal_num deal_date trader maturity log_creator DF_234 20-5-2008 tman 20-5-2009 tman... (2 Replies)
Discussion started by: magikminox
2 Replies
Login or Register to Ask a Question