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…
1
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
1
If Perl is an option, you can use this Perl one-liner:
echo "20240801T00:48 foo bar" | perl -pe 's{^(S+)S}{$1}'
20240801T00:4 foo bar
The Perl one-liner uses these command line flags:
-e
: Tells Perl to look for code in-line, instead of in a file.
-p
: Loop over the input one line at a time, assigning it to $_
by default. Add print $_
after each loop iteration.
s{PATTERN}{REPLACEMENT}
: Replace regex PATTERN with REPLACEMENT.
^
: Matches the beginning of the line.
(S+)S
: Matches the maximum number of non-whitespace characters, captured using parentheses into capture group 1 (later referenced with $1
), followed by one (last) non-whitespace character (which is not captured, so does not get included into $1
).
See also:
perldoc perlrun
: how to execute the Perl interpreter: command line switchesperldoc perlrequick
: Perl regular expressions quick start
This might work for you (GNU sed):
sed -E 's/(S+)S/1/' file
If a word must contain at least 2 non-white space characters, the above solution will remove the last character.
If however a word may contain at least 1 non-white space characters, then use:
sed -E 's/(S*)S/1/' file