-1

Ok, so I have a text file formatted like this, it has several rows going down.

EXAMPLE1:EXAMPLE2:EXAMPLE3
EXAMPLE1:EXAMPLE2:EXAMPLE3
EXAMPLE1:EXAMPLE2:EXAMPLE3

I want to use grep to change it to look like this

EXAMPLE2:EXAMPLE3

Basically I want to take out the first part before the :

If someone could please tell me how to do this it would be greatly appreciated.

nu11p01n73R
  • 25,677
  • 2
  • 36
  • 50
Sprint
  • 1
  • Possible duplicate of [How to grep and replace](http://stackoverflow.com/questions/15402770/how-to-grep-and-replace) – tripleee Jun 11 '16 at 08:08
  • Do you want to take out all first fields or only when it contains `EXAMPLE1` at the start of the line. In that case you can use something like `sed 's/^EXAMPLE1://' textfile`. – Walter A Jun 12 '16 at 14:39

4 Answers4

2

You can use the cut utility to filter out columns given a delimiter.

In this example, the delimiter is : and you want every column starting with the second:

$ cut -d: -f2- < input.txt
Bo Borgerson
  • 1,301
  • 9
  • 19
1

You can use -o flag to select the part of the input which matches a regex in grep.

Example

$ grep -o "[^:]*:[^:]*$" input
EXAMPLE2:EXAMPLE3
EXAMPLE2:EXAMPLE3
EXAMPLE2:EXAMPLE3
nu11p01n73R
  • 25,677
  • 2
  • 36
  • 50
0

Another way to do this. Hope this helps.

awk -F":" '{printf "%s:%s\n" ,$1,$2}' file.txt

Output

EXAMPLE1:EXAMPLE2
EXAMPLE1:EXAMPLE2
EXAMPLE1:EXAMPLE2
sumitya
  • 2,543
  • 1
  • 17
  • 30
-1

Here it is:

cut -d':' -f2,3 filename

Output:

EXAMPLE2:EXAMPLE3
EXAMPLE2:EXAMPLE3
EXAMPLE2:EXAMPLE3