Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-17-2008, 08:27 AM   #1
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 ...

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

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
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
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

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

Rep: Reputation: 16


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

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

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
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46

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:
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
Old 03-04-2009, 01:37 AM   #6
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.


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
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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:07 PM.

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