LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-17-2008, 08:27 AM   #1
endfx
Member
 
Registered: May 2003
Location: Canada
Distribution: Debian
Posts: 105

Rep: Reputation: 15
Need 'cut' with mulit char delimiter


I need to tokenize a very long line of characters. Cut would work perfectly, however I need to use more than one character for the delimiter.

Does anybody have any ideas?
I don't have perl, but I do have most of the standard linux command line tools available: cut, sed, awk ...

Thanks
 
Old 11-17-2008, 09:18 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Well, I'm probably not familiar with many programming term/jargon, but could you provide a more concrete example of what needs to be done ? I personally don't get it.
 
Old 11-17-2008, 09:20 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can try to use awk, since the Field Separator can be a single character or a regular expression. For example if I have
Code:
$ cat testfile
Hellodelimworlddelim!!!
and I want to split this line using "delim" as delimiter:
Code:
$ awk -F"delim" '{print $1; print $2; print $3}' testfile
Hello
world
!!!
How to split fields and how to store the result, depends from your specific needs.
 
Old 11-17-2008, 09:30 AM   #4
zer0x333
Member
 
Registered: Oct 2007
Posts: 31

Rep: Reputation: 16
awk

Hi,

I just had a quick play with awk, looks the the field separator option can take multiple arguments!?

e.g. ',' and '=' using OR ..

Code:
cat inputfile | awk -F \(,\|=\) '{print $2}'
Please excuse the bad example! xD

HTH, zer0x

Last edited by zer0x333; 11-17-2008 at 09:33 AM. Reason: Lol, too slow xF
 
Old 11-17-2008, 01:42 PM   #5
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

you mean, the delimiter is not a multi character string, but is some times one character, another time a different one? In ths case the simpliest way would be to redefine IFS (Input Field Separator) and then use a simple loop:
Code:
jan@jack:~/tmp> IFS=',=
> '
jan@jack:~/tmp> echo "a,b=c" | while read f1 f2 f3; do
> echo "f1=$f1 f2=$fs f3=$f3"
> done
f1=a f2= f3=c
jan@jack:~/tmp> echo "a,b=c" | while read f1 f2 f3; do echo "f1=$f1 f2=$f2 f3=$f3"; done
f1=a f2=b f3=c
Jan
 
Old 03-04-2009, 01:37 AM   #6
virteman
LQ Newbie
 
Registered: Aug 2008
Posts: 3

Rep: Reputation: 0
cut's delimiter must be a single character. You can replace your long character list to a special character first, then use cut.
 
  


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
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
Mulit-boot questions chemdawg Linux - General 10 09-27-2005 09:35 PM
Mulit Language Support zaicheke Slackware 1 11-20-2004 12:10 AM
mulit users in x square_eyes Linux - Newbie 5 04-24-2003 08:45 PM

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

All times are GMT -5. The time now is 03:55 PM.

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