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!
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!
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
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
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 colonSo '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