Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim specific text with grep

Tags:

linux

grep

bash

awk

I am in need of trimming some text with grep, I have tried various other methods and havn't had much luck, so for example:

C:\Users\Admin\Documents\report2011.docx: My Report 2011
C:\Users\Admin\Documents\newposter.docx: Dinner Party Poster 08

How would it be possible to trim the text file, so to trim the ":" and all characters after it.

E.g. so the output would be like:

C:\Users\Admin\Documents\report2011.docx
C:\Users\Admin\Documents\newposter.docx
like image 477
James Avatar asked Dec 03 '22 08:12

James


1 Answers

use awk?

awk -F: '{print $1':'$2}' inputFile > outFile

you can use grep (note that -o returns only the matching text)

grep -oe "^C:[^:]" inputFile > outFile 
like image 140
matchew Avatar answered Dec 11 '22 15:12

matchew