LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-04-2010, 05:20 PM   #1
General
Member
 
Registered: Aug 2005
Distribution: Debian 7
Posts: 526

Rep: Reputation: 31
Tool for replacing first match per line


I've got a file that I want to convert to a CSV format.

Each line of the file is something like:

term1a term1b [pronunciation] definition
term2a term2b [pronunciation] definition
term3a term3b [pronunciation] definition

To get it into CSV, I just need to perform 3 find and replace operations:

First, replace the first " " (space) with a "," (comma).
Next, replace the first " [" (space and right-facing square bracket) with a "," (comma).
Finally, replace the first "] " (left-facing square bracket and a space) with a "," (comma).

The reason it must only be the first such occurance, is the definition area contains lots of spaces and square brackets which I'd like to leave left untouched.
 
Old 05-04-2010, 05:38 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by General View Post
I've got a file that I want to convert to a CSV format.

Each line of the file is something like:

term1a term1b [pronunciation] definition
term2a term2b [pronunciation] definition
term3a term3b [pronunciation] definition

To get it into CSV, I just need to perform 3 find and replace operations:

First, replace the first " " (space) with a "," (comma).
Next, replace the first " [" (space and right-facing square bracket) with a "," (comma).
Finally, replace the first "] " (left-facing square bracket and a space) with a "," (comma).

The reason it must only be the first such occurance, is the definition area contains lots of spaces and square brackets which I'd like to leave left untouched.
Ok...I'd definitely use 'sed' to do that. You should easily be able to write a script to do it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert line on match only once with sed? lhouk Programming 38 08-10-2010 09:23 AM
[SOLVED] Adding (not replacing) a pattern match with a similar pattern? b-bri Linux - Newbie 2 08-31-2009 12:36 AM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration