Extract string from a column

A 5 star hangout for overworked and underpaid system admins.
Post Reply
Kinjal
Posts: 7
Joined: 2016/05/24 12:08:33

Extract string from a column

Post by Kinjal » 2016/05/24 12:11:01

Hi,
I have record with three columns
ID TYPE Deal
01 A Member Since Year-2015
02 B Member Since Year-2014
03 A Member Since Year-2014
I only want year part from field Deal How to write a query?
Thanks,

User avatar
TrevorH
Forum Moderator
Posts: 26311
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Extract string from a column

Post by TrevorH » 2016/05/24 23:17:38

This isn't really a CentOS problem as such so I've moved it to the social forum for want of anywhere better to put it.

How are the columns separated?
CentOS 5 died in March 2017 - migrate NOW!
CentOS 6 goes EOL sooner rather than later, get upgrading!
Full time Geek, part time moderator. Use the FAQ Luke

drk
Posts: 397
Joined: 2014/01/30 20:38:28

Re: Extract string from a column

Post by drk » 2016/05/25 02:44:42

Kinjal wrote:Hi,
I have record with three columns
ID TYPE Deal
01 A Member Since Year-2015
02 B Member Since Year-2014
03 A Member Since Year-2014
I only want year part from field Deal How to write a query?
Thanks,
$ awk -F- '{print $NF}' /path/to/file

Just one way

giulix63
Posts: 1305
Joined: 2014/05/14 10:06:37
Location: GMT

Re: Extract string from a column

Post by giulix63 » 2016/05/25 07:18:15

Another way:

Code: Select all

grep -Eo Year-[0-9]{4} /path/to/file |grep -Eo [0-9]{4}
Root is evil: Do not use root (sudo) to run any of the commands specified in my posts unless explicitly indicated. Please, provide the necessary amount of context to understand your problem/question.

aks
Posts: 2807
Joined: 2014/09/20 11:22:14

Re: Extract string from a column

Post by aks » 2016/05/25 15:53:40

cut -d'-' -f 2- <file>

drk
Posts: 397
Joined: 2014/01/30 20:38:28

Re: Extract string from a column

Post by drk » 2016/05/26 05:25:30

sed 's!.*r-!!' /path/to/file

Post Reply