LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-01-2011, 12:04 AM   #1
tennis_slacker
Member
 
Registered: Apr 2008
Location: Idaho
Distribution: Slackware
Posts: 92

Rep: Reputation: 17
sed - get all text after first colon


Hello,

This will probably be a simple question for the experts here, but I've been googling and testing and trying for a few hours with no luck. I'm tired and stumped.

My simple test case:
Code:
$ cat test.txt
First colon: Get everything after: first colon.
I want to return all the text after the first colon. So that would be "Get everything after: first colon."

This is what I currently have:
Code:
$ sed 's/.*: //' test.txt
first colon.
This returns everything after the second colon.

Thanks!
 
Old 03-01-2011, 01:47 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392
Hi,

Your posted result is correct, that is due to the fact that sed is greedy.
Code:
sed 's/[[:alpha:] ]*: //' test.txt
Here's another way of doing it using cut:
Code:
cut -d: -f2- test.txt
Hope this helps.
 
Old 03-01-2011, 02:17 AM   #3
murugesan
Member
 
Registered: May 2003
Posts: 161

Rep: Reputation: 28
Updated sed command:
Code:
       sed 's/[[:alnum:] ]*: //' input.txt
 
Old 03-01-2011, 02:51 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,643

Rep: Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960
As a slight alternative:
Code:
sed 's/^[^:]*: //' file
 
1 members found this post helpful.
Old 03-01-2011, 09:56 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947
I recommend grail's solution. Furthermore, if you want to only print the first occurrence (and skip any lines without colons before that), you can do
Code:
sed -ne '/:/ { s/^[^:]*:[\t\v\f ]*//p ; q0 }' file
(The [\t\v\f ]* in the pattern removes whitespace immediately after the colon.)
 
Old 03-01-2011, 10:11 AM   #6
tennis_slacker
Member
 
Registered: Apr 2008
Location: Idaho
Distribution: Slackware
Posts: 92

Original Poster
Rep: Reputation: 17
Awesome!! Grail's solution works perfectly for my needs. Thanks for everyone's posts!
 
  


Reply


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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 02:48 PM
need sed to output block of text if specified text is matched r0dr1g0 Programming 3 02-13-2010 06:59 PM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 08:54 AM
sed: delete text till <pattern2> depending on length of text oyarsamoh Programming 2 05-05-2007 02:40 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:11 AM.

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