473,659 Members | 3,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Format text file, replace few whitespaces with commas.

2 New Member
Hi All,

I have a textfile and i need to format it, so that i can import it to the mysql database.

The textfile contains 9 colums of data separated by a space. I want to replace only the first 8 whitespaces separating those 9 colums, because the 9th column contains a sentence separated by space. similaryly i want all the rows in that file to get updated in the same fashion.

Heres how the contents of the textfile looks:
-------------------------------------------


2223 A None NULL 5 03/05/2008 mike Not_Apply active directory feature
3232 O 4.5.4 39 7 04/05/2008 tim cisco bitrate enhancement

similaryly many more rows.

Only the first 8 columns needs to be delimited with a comma. I been stuck with this for many hours now. any help would be appreciated.


/Kris
Mar 8 '08 #1
3 2470
ronverdonk
4,258 Recognized Expert Specialist
When you are sure about the correct format of your text file you can[php]
$text=str_repla ce(' ', ' ', $text); // replace all double blanks by one blank
$text=str_repla ce(' ', ',', $text,8); // replace first 8 blanks with comma[/php]Ronald
Mar 8 '08 #2
kris06
2 New Member
When you are sure about the correct format of your text file you can[php]
$text=str_repla ce(' ', ' ', $text); // replace all double blanks by one blank
$text=str_repla ce(' ', ',', $text,8); // replace first 8 blanks with comma[/php]Ronald
But i think i need to read from the start of the file to the end, and make changes line by line. i am not sure, how to accomplish that !. please help.
Mar 8 '08 #3
ronverdonk
4,258 Recognized Expert Specialist
You mean you don't know how to read a fiole line by line? Allright then[php]<?php
$file = "test.txt";
$handle = fopen($file,"r" );
if ($handle) {
while(!feof($ha ndle)){
$text = fgets($handle, 4096); // read one line
$text=str_repla ce(' ', ' ', $text); // replace double blanks by 1 blank
$text=str_repla ce(' ', ',', $text,8); // replace first 8 blanks with comma

}
}
?>[/php]Ronald
Mar 8 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
20687
by: Paul | last post by:
I have a text file that I am trying to display on a web page. If I cat or more the file it formats and displays fine. When it comes up in the browser it seems to loose tabs and the format gets messed up. This is how I display the file. $show = file("./fields/combined/$cdp$store"); $arrayitems = sizeof($show); $x=0; while ($x < $arrayitems) { print("$show\n");
4
8449
by: mrby | last post by:
Hi, I see that Wiki's method of editing HTML is rather simple, for example: Using *bold* for bold text, and using _itatics_ for itatic font text. I wonder if there is available code for this purpose? Thanks much!
1
2748
by: Newgene | last post by:
Hi, group, I have python2.3 installed on win2k. I noticed that when I open a dos format text file (eol is '\r\n'), readline() always returns a line ending with '\n' only, not '\r\n'. While I read the same file on unix, it returns a line ending with '\r\n' correctly. This makes me difficult to determine the format of a text file, dos or unix. Is this a bug or intended behavior? If not a bug, then how to determine the format of a text...
2
5660
by: ezelasky | last post by:
We are using the bcp utility (via APIs) to export data from a SQL table in a fixed format text file. BCP is inserting spaces for a field if the field contains a NULL. This is fine with us except at the end of the line, there are no spaces for that field just the end-of-row terminator prematurely, so it looks like that field is not present and messes up another piece of software we pump the text file into down stream. Example -- The...
4
17470
by: Vic | last post by:
Hi, With VBA code from MSAccess, how to search for a string in another text file, and replace it with space and then save it under the same name? Your help is greatly appreciated. Rgds
1
7512
by: Mike | last post by:
I'm trying to create a text file and when i compile my code i keep getting invalid token FileInfo t = new FileInfo("test.txt"); StreamWriter Tex =t.CreateText(); Tex.WriteLine("test file"); Tex.Write(Tex.NewLine); Tex.close(); the error i'm getting is
1
12714
by: Derek Hart | last post by:
Using VB.NET, I wish to open a text file that will have paragraph marks throughout it. To replace them, I can probably use the Replace command and replace vbcrlf. But can somebody give me sample code using the StreamWriter object, if that is the best way, to open the text file, replace the paragraph marks, and save those changes back to the text file. I am not sure if I should read in the text file line by line, do the replacing, save...
3
1390
by: Kou Vang | last post by:
I have a large text file seperated by commas. I was wondering how to get the values from the text file. The problem is that not all the characters are the same length, so I can't use the characters, but is I can't find how to seperate out the values by the commas. I know that reading the whole line is possible, but what if I just want the third value and not the rest? Thanks. exp: 9821,1550,200,2004
3
1907
by: ajnlax | last post by:
I need to make a single pass through the text file and count entries. These entries are seperated by commas and in 10 seperate strings. I need to exclude comments marked by * characters in the text and replace the commas with Nulls. How do I do this after I have already open the text fiel, malloc a buffer, and read it to memory????
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.