Search code examples
sed

Remove last character of first word


I want to remove the last character from the first word in a stream of strings using sed, e.g., 20240801T00:48 should become 20240801T00:4. I've tried

sed 's/\(.\)\([^ ]*\) /\2

but it doesn't change anything. If I reference group 1 that replaces the whole word with the last character (as you would expect). Not sure what I'm doing wrong, or if there's a better way...


Solution

  • Based on the input shown, you don't really need any capture groups here.

    Just use:

    echo "20240801T00:48 foo bar" | sed 's/. / /'
    
    20240801T00:4 foo bar
    

    Here:

    • . will match any character followed by a space, which will match first space instance only
    • In the replacement we place the space back in the output