LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2005, 08:00 AM   #1
elmu
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Rep: Reputation: 0
Divide up lines with string delimiter


Hello,

I have lines in the following format:
text1^~^text2^~^text3
where the delimiter is ^~^

How can I divide it up to text1, text2, text3?

Thanks!
 
Old 10-07-2005, 08:09 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Assuming you have no extraneous "^" characters You can do it with awk:

Try:
echo text1^~^text2^~^text3 |awk -F^ '{print $1,$3,$5}'

The above will result in:

text1 text2 text3


The "-F" tells it to set the field delimiter to "^". Since your pattern has 2 of these between each field you just print every 2nd field. The "," in the print statement tells awk to separate the output with spaces. You can leave out the "," if you want "text1text2text3" instead.
 
Old 10-07-2005, 08:17 AM   #3
elmu
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks for fast answer!

Unfortunately the text can contain the ^ and the ~ characters too. That's why I want to use the complete ^~^ as delimiter.
 
Old 10-07-2005, 08:48 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Ugh - you're forcing me to think of proper "sed" syntax:

The following will work:
echo text1^~^text2^~^text3 | sed "s/\^~^/ /g"

This would result in:
text1 text2 text3

For "sed"
The "s" (substitute) the string between the first two "/" with the one between the last two (which is just a space).
The "g" (global) says to do it every time it finds the pattern as otherwise it would only do it the first time.
You need the "\" before the first "^" because in regular expressions "^" has the special meaning of "first character in the line and the "\" escapes this meaning so it sees it as a literal rather than special.

Actually sed is a fairly powerful tool - being lazy I only use it when I need to do so and only think about its syntax then.

Last edited by MensaWater; 10-07-2005 at 08:59 AM.
 
  


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
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM
how to divide two string variables user_linux Programming 3 06-20-2005 04:54 PM
regular expresions: java: matching lines with out string zymos Programming 1 05-28-2005 09:17 AM
c++ get() delimiter ashirazi Programming 3 08-06-2004 05:26 AM
tab delimiter codename000 Programming 3 04-04-2003 10:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 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