deleting string up to the first occurrence of cert

2019-08-12 06:59发布

Is there a way to delete all the characters up to the first occurrence of certain character and that character too.

123:abc
12:cba
1234:cccc

and the output would be:

abc
cba
cccc

4条回答
ら.Afraid
2楼-- · 2019-08-12 07:18

Using sed:

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

Or using awk:

awk -F: '{print $2}' file
abc
cba
cccc
查看更多
女痞
3楼-- · 2019-08-12 07:23

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.
查看更多
【Aperson】
4楼-- · 2019-08-12 07:25

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.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-08-12 07:30

You could use cut:

$ cut -d":" -f2- myfile.txt 
查看更多
登录 后发表回答