Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove all characters in each line after the first space in a text file?

I have a large log file from which I need to extract file names.

The file looks like this:

/path/to/loremIpsumDolor.sit /more/text/here/notAlways/theSame/here
/path/to/anotherFile.ext /more/text/here/differentText/here
.... about 10 million times

I need to extract the file names like this:

loremIpsumDolor.sit
anotherFile.ext

I figure my first strategy is to find/replace all /path/to/ with ''. But I'm stuck how to remove all characters after the space.

Can you help?

like image 291
Ryan Avatar asked Nov 16 '25 14:11

Ryan


2 Answers

sed 's/ .*//' file

It doesn't take any more. The transformed output appears on standard output, of course.

like image 181
Jonathan Leffler Avatar answered Nov 19 '25 08:11

Jonathan Leffler


In theory, you could also use awk to grab the filename from each line as:

awk '{ print $1 }' input_file.log

That, of course, assumes that there are no spaces in any of the filenames. awk defaults to looking for whitespace as the field delimiters, so the above snippet would take the first "field" from your log file (your filename) for each line, and output it.

like image 41
Will Avatar answered Nov 19 '25 07:11

Will



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!