3

So I have a textfile of which I want to remove everything to the first colon (including the colon). So for example if this is the input

0000007ba9ec6950086ce79a8f3a389db4235830:9515rfsvk
000000da2a12da3fbe01a95bddb8ee183c62b94d:letmein2x
000000edf3179a1cf4c354471a897ab7f420bd52:heychudi:rbhai
000000f636f0d7cbc963a62f3a1bc87c9c985a04:cornetti
0000010a15f5b9315ef8e113f139fa413d1f2eb2:3648067PY128

Then this should be the output

9515rfsvk
letmein2x
heychudi:rbhai
cornetti
3648067PY128

Note that the second colon in line 3 remain, only from the start of each line to (including) the first column should be removed.

Is there a quick way to do this with grep or awk?

5 Answers5

16

With cut

cut -d: -f2- file

-d sets the separator and -f2- means from the second to the last field.

thanasisp
  • 8,122
  • Thanks thats what I needed! :) What if I only want to keep the first part? EDIT: -f1 is what I need for that, figured it out myself :D – user4042470 Oct 10 '20 at 12:36
  • @user4042470: exactly. and to keep from fields 2 to 4 including the 2 separators: cut -d':' -f 2-4 – Olivier Dulac Oct 13 '20 at 22:28
10

With sed:

sed 's/[^:]*://' input.txt

In words: match a sequence of zero or more non-colon characters followed by a colon and replace them with nothing.

NickD
  • 2,926
4

This could work with grep (PCRE):

 grep -Po "(?<=:).*" file

Output:

9515rfsvk
letmein2x
heychudi:rbhai
cornetti
3648067PY128
1

With bash itself:

$ str=000000edf3179a1cf4c354471a897ab7f420bd52:heychudi:rbhai
$ echo "${str%%:*}"
000000edf3179a1cf4c354471a897ab7f420bd52

I found the bash-way extremely handy in all scripting work and it has replaced using sed in many occasions.

For more information about why this works, please look up in man bash EXPANSION > Parameter expansion.

XavierStuvw
  • 1,119
-2

If the first : is always at position 41 in the file, you can use:

colrm 1 41 <file

or

cut -c42- file

Not the most convenient option since you need to first determine the position of the column, but at least it can also work in the absence of a separator.

terdon
  • 242,166
nezabudka
  • 2,428
  • 6
  • 15