Bash, remove numbers after colon | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Bash, remove numbers after colon

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-12-2013
3therk1ll 3therk1ll is offline
Registered User
 
Join Date: May 2013
Last Activity: 18 April 2014, 8:51 AM EDT
Posts: 57
Thanks: 3
Thanked 0 Times in 0 Posts
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?


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


Last edited by Scrutinizer; 05-12-2013 at 09:01 AM.. Reason: code tags
Sponsored Links
    #2  
Old 05-12-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 20 April 2014, 3:47 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 3,919
Thanks: 39
Thanked 671 Times in 533 Posts
The fastest way is by "parameter expansion". See the "#", "%", "##" and "%%" operators in the man page for bash, like in:


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

I hope this helps.

bakunin
Sponsored Links
    #3  
Old 05-12-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 20 April 2014, 12:29 PM EDT
Posts: 1,598
Thanks: 90
Thanked 452 Times in 409 Posts
What do you meam by "word list"?
An array? A string variable? A here document? A file?
    #4  
Old 05-12-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 8 April 2014, 11:00 AM EDT
Posts: 1,036
Thanks: 61
Thanked 216 Times in 204 Posts
With awk
Code:
awk -F: '{print $1}' file

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

Sponsored Links
    #5  
Old 05-12-2013
3therk1ll 3therk1ll is offline
Registered User
 
Join Date: May 2013
Last Activity: 18 April 2014, 8:51 AM EDT
Posts: 57
Thanks: 3
Thanked 0 Times in 0 Posts
Yeah I just meant a text file such as the one in my first post.
Cheers for the help guys.
Sponsored Links
    #6  
Old 05-12-2013
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 20 April 2014, 3:47 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 3,919
Thanks: 39
Thanked 671 Times in 533 Posts
Quote:
Originally Posted by 3therk1ll View Post
Yeah I just meant a text file
In this case you should read the file line by line and then do what i showed you in a loop, like this:


Code:
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
Sponsored Links
    #7  
Old 05-12-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 20 April 2014, 12:29 PM EDT
Posts: 1,598
Thanks: 90
Thanked 452 Times in 409 Posts
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

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

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sort numbers which has colon (:) in between varu0612 Shell Programming and Scripting 9 09-16-2012 03:31 AM
Remove char after Colon gowrishankar05 Shell Programming and Scripting 3 04-18-2011 08:55 AM
bash aliases and command chaining with ; (semi-colon) star_man Shell Programming and Scripting 2 11-30-2010 01:34 PM
remove the numbers Beginer0705 Shell Programming and Scripting 3 10-03-2009 07:13 AM
remove a colon and number and leaving the rest cdfd123 Shell Programming and Scripting 5 08-06-2008 11:34 PM



All times are GMT -4. The time now is 10:02 PM.