Rok Dolinar
Rok Dolinar

Reputation: 996

Deleting string up to the first occurrence of certain character

Is there a way to delete all the characters up to and including the first occurrence of a certain character?

123:abc
12:cba
1234:cccc

and the output would be:

abc
cba
cccc

Upvotes: 4

Views: 7660

Answers (4)

Gary_W
Gary_W

Reputation: 10360

If the data is in a variable, you can use parameter expansion:

$ var=123:abc
$ echo ${var#*:}
abc
$

The # means to remove the shortest pattern of *: (anything followed by a colon) from the front of the string, as you said in your requirement "delete all the characters up to the first occurrence of certain character + that character", not to get the second field where the delimiter is the colon.

Upvotes: 1

Kun Ling
Kun Ling

Reputation: 2219

use awk

echo "123:abc" | awk -F ":" '{print $2}'
  • -F means to use : as the separator to split the string.
  • {print $2} means to print the second substring.

Upvotes: 1

anubhava
anubhava

Reputation: 785008

Using sed:

sed 's/^[^:]*://' file
abc
cba
cccc

Or using awk:

awk -F: '{print $2}' file
abc
cba
cccc

Upvotes: 6

Mureinik
Mureinik

Reputation: 311163

You could use cut:

$ cut -d":" -f2- myfile.txt 

Upvotes: 1

Related Questions