Domanda

i have a few name that I need to cut out and get the 2nd part of the name

agent-tom
agent-harry
agent-disk-see

I used cut -d "-" -f2 I only manage to get "tom", "harry" and "disk" question: how do I use the cut command in order to cut the 3rd agent so that i could get "disk-see" ??

Thanks

È stato utile?

Soluzione 3

If you're willing to consider tools other than cut:

pax> sed 's/^[^-]*-//' inputFile
tom
harry
disk-see

This command uses the stream editor sed to remove the part of the line from the start up to the first - character. I generally prefer sed for tasks like these since it's more adaptable when doing things other than simple one-level-deep field manipulations.

Having said that, this is a fairly simple task so a slight modification to your cut command will suffice. Simply use -f2- (field 2 onwards) in place of -f2 (field two only):

pax> cut -d'-' -f2- inputFile
tom
harry
disk-see

Altri suggerimenti

cut -d '-' -f 2-

Cuts from 2nd column to end and will get all regardless of dash count.

Adapted from Wikipedia:

To output the second field through the end of the line of each line using the "-" character as the field delimiter:

cut -d "-" -f 2- file
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top