-3

I have a .txt file, which contains different words like blablabla:thatswhationlywant.

The blablabla words are always different. So I need to print out only all characters after the ":" - how can I do it?

Thanks a lot!

usr
  • 9
  • 1
  • 1
    Welcome to SO. Stack Overflow is a question and answer site for professional and enthusiast programmers. The goal is that you add some code of your own to your question to show at least the research effort you made to solve this yourself. – Cyrus Oct 31 '18 at 18:46
  • 1
    Yes, please do some work yourself and come back with a more specific issue – Elisha Senoo Oct 31 '18 at 18:51
  • 1
    What have you *tried*? – Paul Hodges Oct 31 '18 at 18:58
  • 3
    Possible duplicate of [How to cut a string after a specific character in unix](https://stackoverflow.com/q/18397698/608639). The cited question even used the semicolon. Also see [Get string after character](https://stackoverflow.com/q/15148796/608639), [How to grep for contents after pattern?](https://stackoverflow.com/q/10358547/608639), etc. – jww Oct 31 '18 at 21:58

3 Answers3

2

You could use grep like so:

grep -o ':.*' file.txt

but that will include the :

I would probably do it with cut as it will be more performant

cut -d: -f2 file.txt
Nick Ellis
  • 1,048
  • 11
  • 24
0

use cut tool to filter the output of grep. grep ":" file.txt | cut -d ":" -f 2 will only show the text after the character ':' have a look at the manual page for cut for more infomations

-2

One more possibility. It's no better or worse than the grep solutions provided, but if you're one of those people more familiar with the simple applications of sed's s/// operation that grep's -o with \K it might appeal.

$: sed 's/^.*://' in.txt
thatswhationlywant.

If you're not that familiar with sed (check here for more specific details) -

  • s/a/b/ means find pattern a and replace it with b.
  • ^ anchors the search pattern at the beginning of a line
  • . matches any character (again, see details for precise exception cases)
  • * means "0 or more of the preceding pattern", in this case the dot (.), so any number of any character
  • : is a literal colon

So 's/^.*://' means "substitute any/all characters from the beginning of the line up to and including a colon with nothing."

Be aware that a colon is a character, so if there are mulitple colons on a line they will ALL be consumed, because * is "greedy".

$: echo "a:b:c:d"|sed 's/^.*://'
d

If that isn't what you want, sub in [^:] (which means "not a colon", because inside a square-bracket 'character class' the ^ is a 'not') for ., like this:

$: echo "a:b:c:d"|sed 's/^[^:]*://'
b:c:d
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/21291555) – Matt Bierner Nov 01 '18 at 03:41
  • What link are you talking about? – Paul Hodges Nov 01 '18 at 13:15
  • Apparently this answer is under review for "Low Quality Answers". That's reasonable, because it's pretty minimal, but it is a simple and direct alternative way to accomplish what the OP asked - and never used or included a link to anything. Two people have recommended deleting it, possibly because you should always include the relevant referenced material from a link...but there's no link here. XD – Paul Hodges Nov 01 '18 at 13:24
  • 1
    Code only answers also fall into this category. Please provide more context in your answer or explain why this is the correct solution – Matt Bierner Nov 01 '18 at 18:49