Search code examples
bashcut

cut command to retrieve 2nd part of words


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


Solution

  • 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