Search code examples
stringbashawkcut

How can I remove suffix of string in cut result (bash)?


I want to remove all characters after specific char "/" in all result lines of cut command How may I do that using bash commands?

for example if cut -f2 file results :

John.Micheal/22
Erik.Conner/19
Damian.Lewis/40

I need this on output:

John.Micheal
Erik.Conner
Damian.Lewis

Solution

  • You can pipe it to another cut, using / as the field separator:

    cut -f2 file | cut -f1 -d/
    

    Or you could use sed to cut off everything beyond /:

    cut -f2 file | sed 's?/.*??'
    

    Or you could use a single awk with a custom field separator, assuming there are no / in the first field:

    awk -F'[\t/]' '{print $2}' file
    

    If there can be / in the first field then it's better to use the first two suggestions.