Two Laptop Bag
The moose likes Linux / UNIX and the fly likes SED Question (String tokenize) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "SED Question (String tokenize)" Watch "SED Question (String tokenize)" New topic

SED Question (String tokenize)

Serkan Demir
Ranch Hand

Joined: Feb 03, 2005
Posts: 61
Hi guys,
I have a question with SED and its text processing capability.
I have a file with entries similar to below which are separated with ';'.

I want to separate this line into tokens by delimiting ';' and put index numbers before it.
1) 0
2) 20070718095600
3) 514
4) 3

I have been playing with sed, but i am unsuccessful to do this. Could you help me with this?

Thanks lot,


A guy from Turkey<br /><a href="" target="_blank" rel="nofollow"></a>
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

I wouldn't use sed to do a single character replace; I would use "tr", just like

tr \; \\n

The whole thing could look something like this:

for i in `tr \; \\\n < THEFILE`; do echo $n\) $i; n=`expr $n + 1` ; done

Probably someone else could do this more elegantly.

[Jess in Action][AskingGoodQuestions]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

nl does number lines:
I agree. Here's the link:
subject: SED Question (String tokenize)
It's not a secret anymore!