Bash, remove numbers after colon

Hello All,
I was wondering if someone might know how to do this. I have a word list that is format like the example below. I need to take away the :number after that... is there some kind of command I could use to remove them?

123456:5562
password:1507
123456789:989
qwerty:877
12345678:621
111111:570
abc123:450
123123:446
dragon:389
sayang:374

The fastest way is by "parameter expansion". See the "#", "%", "##" and "%%" operators in the man page for bash, like in:

var="12345:12345"
echo ${var%%:*}
echo ${var##*:}

I hope this helps.

bakunin

What do you meam by "word list"?
An array? A string variable? A here document? A file?

With awk

awk -F: '{print $1}' file

With sed

sed 's/ *:.*//' file

Yeah I just meant a text file such as the one in my first post.
Cheers for the help guys.

In this case you should read the file line by line and then do what i showed you in a loop, like this:

while read var ; do
     echo ${var%%:*}
done < /path/to/text.file

Instead of the "echo" insert whatever you want to do with the value. Using "awk" or "sed" (or "perl" or whatever) for the same purpose will be slower because these programs have to be invoked while parameter expansion is done within the shell itself.

If your goal is not solely to produce a modified file (in this case "sed" is the best solution) but to do something inside the shell with the values thus generated you should do it like i told you.

I hope this helps.

bakunin

A shell script is slower than sed and awk and even perl if the input file is big enough.
(BTW in my measurements bash was much slower than ksh.)
Another shell variant is

while IFS=: read var junk
do
  echo "$var"
done < /path/to/text.file

The "other shell variant" is a good one! It didn't come to me to use the IFS when i wrote my answer, but you are right. Thanks.

We may have misunderstood each other. What i meant was this:

while read var ; do
     var=${var%%<some-subst>}
     do_something $var
     do_smth_else $var
     ...
done < /path/to/file

# whereas, very often seen:
while read var ; do
     var=$(echo $var | sed '<some-modification>')
     do_something $var
     do_smth_else $var
     ...
done < /path/to/file

In this case - where you want to extract something from the file and then do something with it inside the script - "sed" (or "awk" or any other external program) will work considerably slower than the shell built-in, regardless of using your or my method. Your method might be even a bit faster than mine, but both methods will be way faster than any external program.

But: if you want to modify the file itself, like in:

sed '<some-mod>' /path/to/input > /path/to/output

it will be the other way round: "sed" and "awk" will be fastest, "perl" considerably slower and shell only probably the slowest ("ksh" perhaps faster than "bash", because "bash" is bloated beyond belief).

I hope this helps.

bakunin