2

I want to print, for each line of a given document, the first names and the ages of people (which are entries of the document). What linux command would I use? Hopefully based around grep using a regex pattern.

Example of input document:

Linda:F:Las Vegas, USA:18:telnet  
Steve:M:Leipzig, Germany:33:ftp
Henry:M:Milan, Italy:19:sftp 
Alice:F:Izmir, Turkey:32:pop3s
Robert:M:Verona, Italy:21:scp
Olga:F:Gorontalo, Indonesia:19:pop3s
Kimberly:F:Lille, France:25:ping
Henry:M:Oslo, Norway:26:ftp
Carol:F:New Delhi, India:23:pop3s
Thomas:M:New Delhi, India:32:http
  • What have you tried so far? Where are you blocked? You have to show some research effort if you want help from people here. :-) – lgeorget Jul 18 '14 at 11:49
5

You can use cut:

cut -d: -f1,4 file
Linda:18

Steve:33

Henry:19

Alice:32

Robert:21

Olga:19

Kimberly:25

Henry:26

Carol:23

Thomas:32
| improve this answer | |
4

You can use awk:

$ awk -F':' '{print $1,$4}' file
Linda 18

Steve 33

Henry 19

Alice 32

Robert 21

Olga 19

Kimberly 25

Henry 26

Carol 23

Thomas 32
| improve this answer | |
1

sed 's/\([^:]*:\)[^:].*:\([^:]*\):.*/\1\2/' < file

Linda:18
Steve:33
Henry:19
Alice:32
Robert:21
Olga:19
Kimberly:25
Henry:26
Carol:23
Thomas:32
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.