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) and s/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. – dave_thompson_085 Dec 31 '17 at 6:38

To construct a character range from a concatenation of POSIX character classes and literal characters such as . you need something like


and to negate it


So to remove (replace with nothing) all characters except alphanumerics, decimal digits, and periods:

sed 's/[^[:alpha:][:digit:].]//g' somefile
  • Or tr -d '[:alpha:][:digit].' for a different Q :) – dave_thompson_085 Dec 31 '17 at 6:41
  • @steeldriver Thanks for your quick answer. I have trying to build my command based on your exemple: NAME1=$(sed -n '1p' 's/[^[:alpha:][:digit:][:blanck:][:punct:].]//g' textfile and on a debian, it return me this following error message: command.sh: 1: [:sed: unexpected operator, i don't think i have forget something.. – Rom Dec 31 '17 at 10:37
  • @Rom, Try name=$(sed -n '1p;s/[^[:alpha:][:digit:][:blank:][:punct:].]//g' textfile);echo "$name" – ctac_ Dec 31 '17 at 12:49
  • @Rom what are you actually trying to do? what is the purpose of the -n 1p that you have added? Do you want to print the first line regardless? If you just want to remove all characters except those given from all lines, you should be able to use the command I posted without modification. – steeldriver Dec 31 '17 at 13:28
  • Ok thanks it has works fine, but my first actual command to check if a certain line (for exemple the second or the third) is writed is this followed: if [ sed -n "1p" file.txt ]; then....., have u a better idea ? :) – Rom Dec 31 '17 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.