Search code examples
unixawksed

Delete part of a string after a match, but not the line using sed


I have a file a.com as

N-N3-0.0012   -1  5  6  7  L
C-CT-0.002225 -1  5  8  9  H H-3-1
H-H-0.003534  -1  6  3  4  L

I want a.com to modify as

N  -1  5  6  7  L
C  -1  5  8  9  H H-3-1
H  -1  6  3  4  L

Basically, I want to delete everything after the first "-" match in the first column, but retain all other columns.

I can do this by extracting first column into a file and then using sed to delete everything after the match "-" and replace that with first column in the original file. But I was wondering is there a easy command with sed, for not to delete rest of the line, but rest of the column after a match.


Solution

  • like this?

    kent$  awk '{sub(/-.*/,"",$1)}7' a.com
    N -1 5 6 7 L
    C -1 5 8 9 H H-3-1
    H -1 6 3 4 L
    

    sed:

    kent$  sed 's/-\S*//' file
    N   -1  5  6  7  L
    C -1  5  8  9  H H-3-1
    H  -1  6  3  4  L
    

    if you want to keep the format:

    kent$  sed 's/-\S*\s*/ /' file
    N -1  5  6  7  L
    C -1  5  8  9  H H-3-1
    H -1  6  3  4  L