Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the first word of each line in my file using the linux commands?

Tags:

linux

grep

shell

I have a file containing many lines, and I want to display only the first word of each line with the Linux commands.

How can I do that?

like image 802
MOHAMED Avatar asked Mar 15 '13 14:03

MOHAMED


People also ask

How do I print the first word in Linux?

To print a whole word, you want -f 1 , not -c 1 . And since the default field delimiter is TAB rather than SPACE, you need to use the -d option.

How do I put text at the beginning of a line in Linux?

The ^ character is what instructs the sed command to add a character to the beginning of each line. Here's the syntax for adding a space to the beginning of each line using sed . Alternatively, use the -i option with the sed command to edit a file in place.

How do I display the first few lines of a file in Linux?

To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press <Enter>. By default, head shows you the first 10 lines of a file.


1 Answers

Try doing this using grep :

grep -Eo '^[^ ]+' file 
like image 58
Gilles Quenot Avatar answered Sep 28 '22 09:09

Gilles Quenot