Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert columns to tab separated lines

Posted on 2004-07-30
4
Medium Priority
?
240 Views
Last Modified: 2006-11-17
I have in a file a column.
**********
red
green
blue
yellow
**********
black
purple
violet
brown
gold
**********



I want to move this to a new file and tab separate lines.

red \t green \t blue \t yellow
black \t purple \t violet \t brown \t gold

Moving to the new file I can do.
How do I make a tab separated lines and omit the ********** ??
0
Comment
Question by:Ted22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 11682502
For the purpose of example i have put the original data you provided into a variable:

$var = "
**********
red
green
blue
yellow
**********
black
purple
violet
brown
gold
**********
";

Theres two main ways you can tackle this, one is string replacement, eg.

//replace new lines with tabs and strip out *
$var = str_replace("\n","\t",$var);
$var = str_replace("*","",$var);

The second way, and the one i would use, is to use a simple preg_split to procuce nice tidy output

$var = preg_split("/[\s\*]+/",$var,-1,PREG_SPLIT_NO_EMPTY);
$var = implode(" \t ",$var);

use echo $var; to test the output if needed

Diablo
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 500 total points
ID: 11690655
About Diablo84 first method, doesn't work as expected because you are eliminating ALL newlines. It would be better:

$var = str_replace("\n","\t",$var);    // Replace all newlines by tabs
$var = str_replace("**********","\n",$var);    // Replace asterisks separator with a newline

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question