4

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?

3
  • Not with grep but certainly with sed. Commented Oct 10, 2020 at 1:30
  • Yeah, I keep forgetting about -o and my knowledge of regexps predates perl :-) Commented Oct 10, 2020 at 13:23
  • @agc That is not a good duplicate target as there is only one / in the target. Commented Oct 12, 2020 at 12:45

5 Answers 5

17

With cut

cut -d: -f2- file 

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

2
  • 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 Commented Oct 10, 2020 at 12:36
  • @user4042470: exactly. and to keep from fields 2 to 4 including the 2 separators: cut -d':' -f 2-4 Commented Oct 13, 2020 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.

4

This could work with grep (PCRE):

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

Output:

9515rfsvk letmein2x heychudi:rbhai cornetti 3648067PY128 
1
  • Or grep -oP ':\K.*' Commented Apr 16, 2022 at 14:34
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.

1
-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.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.