I have a text file and the data inside has format like (name age country):

michael jordans 25 US
adam smith 30 UK
chris wood ABC 22 Aus

if I use command: cat text.txt | awk {'print $1'} --> it will print:


but I want to print the full name:

michael jordans
adam smith
chris wood ABC

Which command should I use ?

We can change the format data like:

michael jordans|25|US
adam smith|30|UK
chris wood ABC|22|Aus

2 Answers 2

sed 's/^\([^0-9]*\).*/\1/' text.tex

This assumes that the name does not contain a numeric character and the field just after the name starts with a number.

When you separate the fields by |, it can be done by

sed 's/^\([^|]*\).*/\1/' text.tex

or if you like awk, you can do

awk -F\| '{print $1}' text.tex

If you change the file to have proper field separators, like |, the solution is trivial:

awk -F'|' '{print $1}' input_file

If you use blanks, which also occur within the intended fields, instead of proper field separators, then you can do something like this:

 awk '{ a = $1; for (i=2; i <= NF-2; i++) a = a " " $i; print a; }' input_file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .