Suggestions for converting a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Suggestions for converting a file
# 1  
Old 10-01-2008
Suggestions for converting a file

I am trying to convert a list of information that has 6 or 7 lines in it, into a delimited file. The file looks like this:

Company
Contact
Address
City, St, Zip
Phone
email
website (optional)
<blank line b/t records>


I would like the output to be:

Company,Contact,Address,City, St, Zip,Phone,email,website (optional)

Any thoughts or ideas on how I can do this?

Thanks!
# 2  
Old 10-01-2008
Try this.....

nawk 'BEGIN{OFS=","}{printf " %s ", $1}' file

Regards
# 3  
Old 10-01-2008
Thanks... however, it is creating one field in one record not 7 fields per record (multiple records)

:-(

I appreciate your help!
# 4  
Old 10-01-2008
if you have PHP
Code:
<?php

$file = "file";
$data = split("\n",file_get_contents($file));
$data = array_filter($data);
$data = array_chunk($data,7);
foreach ( $data as $k=>$v){
  echo implode(",",$v)."\n";  
}
?>

# 5  
Old 10-01-2008
Another one,

Code:
awk -F'\n'  '$1=$1' OFS=',' RS=  file

# 6  
Old 10-01-2008
Perl script:
Code:
#!/usr/bin/perl
# You may need to change this path to /usr/local/bin/perl

open (datafile,"file.txt");
@lines = <datafile>;
close (datafile);

open (datafile,">newfile.txt");
foreach $l (@lines) {
  if ($l eq "\n") {
      print datafile $l;
    }
    else {
      chomp($l);
      print datafile "$l,";
  }
}
close (datafile);

exit;

As is, each line will end with a comma. Slight editing can remove that.
# 7  
Old 10-02-2008
With the perl script is there an easy way to get it to start a new record after the 8th iteration. Does that make sense? What I want is to take rows 1-8 and put them on a new line, then have rows 9-16 written out as line 2, ect...

Thanks again for your suggestion.

PG
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading and Writing a conf file - Suggestions and improvements?

Hello all As part of my TUI - (line based) Text User Interface, i do have 2 commands to assist working with conf files. Now, for me they work, but since i wrote them, i automaticly use them they way they should be used... you know what i mean. ;) Anyway, they are designed to read 'simple'... (3 Replies)
Discussion started by: sea
3 Replies

2. Shell Programming and Scripting

Converting an scim .bin.user file to a stardict tab file possible with awk?

Hi all, Here is a scim sample.bin.user file a string1 0 a string2 0 a string3 63 b string4 126 c string5 315 d string6 0 e string7 63 e string8 126 f string9 0 I like to convert this into a dict.tab file to be compiled by the ... (4 Replies)
Discussion started by: hk008
4 Replies

3. UNIX for Dummies Questions & Answers

cleaning up spaces from fixed width file while converting to csv file

Open to a sed/awk/or perl alternative so that i can stick command into my bash script. This is a problem I resolve using a combination of cut commands - but that is getting convoluted. So would really appreciate it if someone could provide a better solution which basically replaces all... (3 Replies)
Discussion started by: svn
3 Replies

4. Shell Programming and Scripting

Suggestions for adding columns to text file

Good afternoon to everyone, I have some input and output from various widgets that I am trying to get to play nicely together. Basically I would like to stay out of excel and be able to automate the entire process. I have read some posts here about how to use awk, nawk, etc, to do similar... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

5. UNIX for Advanced & Expert Users

Converting .csv file into .xls file and send it to inbox

Hi All, I wrote a script to extract data from Oracle DB and place it in a text file , and I have coverted .txt file into comma seperated .csv file and I sent it to my mail box . I can get .xls file in my inbox.I am getting all data in same column and in different rows , without column... (1 Reply)
Discussion started by: krthkmuthu
1 Replies

6. Shell Programming and Scripting

Suggestions on constructing a log file

Hi, I have a series of BASH shell scripts that run together. I would like it so that these scripts construct a log file as they run. This log file would ideally be a text file that contains exactly (including blank lines) what is output to the terminal. What is the best way to accomplish... (3 Replies)
Discussion started by: msb65
3 Replies

7. Shell Programming and Scripting

converting fixed length file to delimited file

hi , i need to convert fixed length file to delimited file using unix where length of each column is variable (2 Replies)
Discussion started by: Nishithinfy
2 Replies

8. UNIX for Advanced & Expert Users

Problem in converting password protected excel file to csv file in unix

I need to convert a password protected excel file which will be in UNIX server to a comma separated file. For this I need to open the excel file in UNIX box but the UNIX box doesn't prompt for password instead it is opened in an encrypted manner. I could manually ftp the excel file to local... (2 Replies)
Discussion started by: Devivish
2 Replies

9. UNIX for Advanced & Expert Users

converting a .txt file to comma delimeted file

Dear all, I have a file with 5L records. one of the record in the file is as shown below. MARIA THOMAS BASIL 1000 FM 1111 MD ... (1 Reply)
Discussion started by: OSD
1 Replies

10. Shell Programming and Scripting

Converting a Delimited File to Fixed width file

Hi, I have a delimited file generated by a database and i need to convert it to fixed width file using the field length of the database. Can any body suggest me how can i proceed with it? :confused: Thanks Raghavan (2 Replies)
Discussion started by: raghavan.aero
2 Replies
Login or Register to Ask a Question