0

I am new to bash scripting. I am trying to write a script which searches for a four letter/number string which has the form 'Jxxx' where x can be A-Z or 0-9. How do I represent this as a variable in bash? Thanks very much

1
  • 2
    You should read some regex tutorial. Your problem is typical regex search problem. e.g. grep 'J[0-9A-Z]\{3\}' file Commented Sep 13, 2018 at 9:06

1 Answer 1

1

Variables just contain static content in Bash - what you want is a regular expression, aka. "regex", which in your case would be simplest expressed as J[A-Z0-9][A-Z0-9][A-Z0-9]. You can use this with many programs to match text in files:

$ cat > my.txt << EOF
JAA
JAAA
JZ0Z
J00
foo
EOF
$ grep 'J[A-Z0-9][A-Z0-9][A-Z0-9]' my.txt
JAAA
JZ0Z

or filenames:

$ touch JAA JAAA JZ0Z J00 foo
$ find . -name 'J[A-Z0-9][A-Z0-9][A-Z0-9]'
./JZ0Z
./JAAA
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.