Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing only the first field in a string

Tags:

unix

sed

field

awk

cut

I have a date as 12/12/2013 14:32 I want to convert it into only 12/12/2013. The string can be 1/1/2013 12:32 or 1/10/2013 23:41 I need only the date part.

like image 533
user2099444 Avatar asked Feb 22 '13 12:02

user2099444


People also ask

How do you get to the first field in awk?

awk to print the first column. The first column of any file can be printed by using $1 variable in awk. But if the value of the first column contains multiple words then only the first word of the first column prints. By using a specific delimiter, the first column can be printed properly.

How do I print the first column in Unix?

Here, \t is used as a field separator to print the first column of the file. The '-F' option is used to set the field separator.

How do I print the first row in Linux?

head with the option "-1" displays the first line. 2. The best of all options since it uses an internal command. read command is used to take user input from the standard input in shell.


1 Answers

You can do this easily with a variety of Unix tools:

$ cut -d' ' -f1  <<< "12/12/2013 14:32" 12/12/2013  $ awk '{print $1}' <<< "12/12/2013 14:32" 12/12/2013  $ sed 's/ .*//' <<< "12/12/2013 14:32" 12/12/2013  $ grep -o "^\S\+"  <<< "12/12/2013 14:32" 12/12/2013  $ perl -lane 'print $F[0]' <<< "12/12/2013 14:32" 12/12/2013 
like image 72
Chris Seymour Avatar answered Oct 03 '22 01:10

Chris Seymour