By using sed (or grep if sed doesn't support functions), I am searching how to copy only letters, points (.), spaces, and numbers on a specific line of text file and send the sed output to a var; I don't want to cut part of the text.
I want sed to just read the specified line of the text file, and copy only the a-z, A-Z, 0-9 characters and ". " symbols.
By browsing several webpages and the GNU man page of SED: https://www.gnu.org/software/sed/manual/sed.html
I am trying to do that with this command:
NAME1=$(sed -n '1p' -e 's/.*\[:alpha:digit:blank:punct:]/`\1/p`' txtfile)
But i get this an not really cleared error message:
test.sh: 1: [: sed: unexpected operator
I don't remember how to indicate several types of "Character Classes and Bracket Expressions" in a sed command, and also I am not sure of my usage of -n
option location to indicate to sed on which line number of text it must work.
I use \1/p
argument/option to indicate to sed that the matched pattern(s) (if sed finds something) must be kept in the original file.
\1/p
in backquotes won't work at all (I assume you copied/entered that wrong) ands/pattern/\1/p
is really a combination of two things: replace the matched data with the first match group, and after completing the substitution print the resulting line. But it won't work here because you didn't specify any match group(s). If you do specify a group like's/.*\([goodchars]\+.*\)/\1/p'
then the command will run but it will select only one char per line not all the 'good' ones.