Issue
I tried to remove the unwanted symbols
%H1256
*+E1111
*;E2311
+-'E3211
{E4511
DE4513
so I tried by using this command
sed 's/+E[0-9]/E/g
but it won't remove the blank spaces, and the digits need to be preserved.
expected:
H1256
E1111
E2311
E3211
E4511
E4513
EDIT
Special thanks to https://stackoverflow.com/users/3832970/wiktor-stribiżew my days have been saved by him
sed -n 's/.*\([A-Z][0-9]*\).*/\1/p' file or grep -oE '[A-Z][0-9]+' file
Solution
You may use either sed
:
sed -n 's/.*\([[:upper:]][[:digit:]]*\).*/\1/p' file
or grep
:
grep -oE '[[:upper:]][[:digit:]]+' file
See the online demo
Basically, the patterns match an uppercase letter ([[:upper:]]
) followed with digits ([[:digit:]]*
matches 0 or more digits in the POSIX BRE sed
solution and [[:digit:]]+
matches 1+ digits in an POSIX ERE grep
solution).
While sed
solution will extract a single value (last one) from each line, grep
will extract all values it finds from all lines.
Answered By - Wiktor Stribiżew