Not all those spaces between the columns look to be tabs, so cut
won't be able to do what you want. I'd suggest using awk
instead. It's more flexible than cut
when parsing columns of data such as what you're trying to accomplish:
$ awk '{print $3,$4,$5,$8}' data.txt
Example
$ awk '{print $3,$4,$5,$8}' data.txt
4567 Harrison Joel Accountant
4587 Mitchell Barbara Admin
3589 Olson Timothy Supervisor
4591 Moore Sarah Dept
4527 Polk John Accountant
4567 Harrison Joel Accountant
1557 Harrison James Supervisor
You can also space the output by using the column
command:
$ awk '{print $3,$4,$5,$8}' data.txt |column -t
4567 Harrison Joel Accountant
4587 Mitchell Barbara Admin
3589 Olson Timothy Supervisor
4591 Moore Sarah Dept
4527 Polk John Accountant
4567 Harrison Joel Accountant
1557 Harrison James Supervisor
You can also do everything using just awk
and printf
:
$ awk '{printf "%s\t%-20s\t%s\n",$3,$4" "$5,$8}' data.txt
4567 Harrison Joel Accountant
4587 Mitchell Barbara Admin
3589 Olson Timothy Supervisor
4591 Moore Sarah Dept
4527 Polk John Accountant
4567 Harrison Joel Accountant
1557 Harrison James Supervisor
cut revisited
The above methods do an OK job, but they don't handle any of the lines where there are spaces within the value for a particular column. For example the line with "Dept Manager" get's chopped to just Dept.
If the data can be guaranteed to be structures as shown we could use cut
but instead of splitting on a delimiter, we could just display using the actual positions of the characters.
Example
This will cut the text from the data.txt
file and print whatever is at positions 9 through 13, and 14 through 35, etc.
$ cut -c 9-13,14-35,43-58 data.txt
4567 Harrison Joel Accountant
4587 Mitchell Barbara Admin Asst
3589 Olson Timothy Supervisor
4591 Moore Sarah Dept Manager
4527 Polk John Accountant
4567 Harrison Joel Accountant
1557 Harrison James Supervisor
awk revisited
Awk can also be made to pull text out based on it's position rather than by a delimiter. It's more verbose though, but here's how, just for completeness.
$ awk '{
printf "%s\t%-20s\t%s\n",substr($0,9,5),substr($0,14,22),substr($0,43,16)
}' data.txt
4567 Harrison Joel Accountant
4587 Mitchell Barbara Admin Asst
3589 Olson Timothy Supervisor
4591 Moore Sarah Dept Manager
4527 Polk John Accountant
4567 Harrison Joel Accountant
1557 Harrison James Supervisor
awk FIELDWIDTHS
If you're using a variant of GNU awk
you can use the variable FIELDWIDTHS
to specify the static size of each field. This works out to be much cleaner than the substr
method, if you have access to it. Also you can effectively glue together fields that would otherwise be parsed as separate fields.
$ awk 'BEGIN { FIELDWIDTHS="4 4 5 24 5 16 11" }{ print $3,$4,$5,$6 }' data.txt
4567 Harrison Joel M 4540 Accountant
4587 Mitchell Barbara C 4541 Admin Asst
3589 Olson Timothy H 4544 Supervisor
4591 Moore Sarah H 4500 Dept Manager
4527 Polk John S 4520 Accountant
4567 Harrison Joel M 4540 Accountant
1557 Harrison James M 4544 Supervisor
od -a
that there really is a tab character separating your fields.