Matching a pattern between two characters (sed)

Hi there,

I have a file with lines like these:
0105:ffff0000:001b:01f4:25:434
0299:ffff0000:0009:01f4:2:319
02d2:ffff0000:000e:01f4:2:507

The above values are split up using ":" characters.
I would like capture each value, no matter what length.

Take for example the first line 0105:ffff0000:001b:01f4:25:434.
First value: 0105 (should be captured from the start of the line until the first ":")
Second value: ffff0000 (everything between the first ":" and the second ":")
Third value: 001b (everything betweed the second ":" and the third ":")
and so on..
There is always 6 values split up between 5 ":"-characters.

I tried the following for the first value:

while read line
do
  first=`echo $line | sed "s/^\(....\).*/\1/"`
  echo $first
  #second=`echo $line | sed ??`
  #third=`echo $line | sed ??`
  #fourth=`echo $line | sed ??`
  #fifth=`echo $line | sed ??`
  #sixth=`echo $line | sed ??`
done<file.txt

This will print the first four characters from the line for the $first var.
The first value is always four characters long, but the rest of the values can differ in length.
That's why I need to do the above approach by only looking between the ":"-characters.
Is it possible by only using sed?

Thanks in advance

first=$(awk 'BEGIN{OFS=FS=":"}$2{print $2}' file)
echo $first

you can modify the field accordingly as well as the echo to bring you one value at a time

The script is going to run on a busybox-enviroment.
The awk application is not available, only sed and grep.

i don't know why you are using sed command.

you mention that data is already exist.
then you can use awk command like this.

first_val=`cat filename|head -1|awk -F":" '{print $1}'`
sec_val=`cat filename|head -1|awk -F":" '{print $2}'`
thrd_val=`cat filename|head -1|awk -F":" '{print $3}'`

if you want for a file then use head $variable in loop.
if you have any doubt please send a mail.

regards
rajesh

I'm afraid the environment doesn't ship with "awk".
So there is no way to do the same thing with only sed?
If not then I'll have to ask the software-maintainer to recompile a new build for our platform including awk.

$ while read line
> do
> echo "$line" | sed 's/:/ /g' | while read a b c d e f
> do
> echo $a
> echo $b
> echo $c
> echo $d
> echo $e
> echo $f
> done
> done <  input_file
while IFS=: read a b c d e f junk
do
    echo "a->[${a}]"
    .....
done < myFile

vgersh99,

Thanks , I was wondering there will be a short way and you showed how it is.

Thanks allot folks!

You really helped me out, especially the example without using sed!
Thanks a million!