LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,953
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
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
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,953
Blog Entries: 5

Rep: Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755Reputation: 755
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


All times are GMT -5. The time now is 01:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration