Solving Unix Script Problem: Finding #s w/ 5 1-4321

  • Thread starter Bimpo
  • Start date
  • Tags
    Unix
In summary, to find numbers with 5 digits in a Unix script, you can use the grep command with the regular expression pattern "\b[0-9]{5}\b". Other regular expressions can also be used, such as "\b[0-9]{5,}\b" to find numbers with at least 5 digits. To output the numbers with 5 digits to a separate file, you can use the output redirection operator ">" with the grep command. The sed command can also be used to find numbers with 5 digits, using the regular expression pattern "\b[0-9]{5}\b". Even if the numbers have leading zeros, they will still be counted as 5 digits.
  • #1
Bimpo
9
0

Homework Statement



Trying to find how many numbers contains 5 between 1 to 4321

Homework Equations



n/a

The Attempt at a Solution



#!/bin/bash
typeset -i current=1
typeset -i times=0
while [ $current -le 4321 ] ; do
if [ ] ; then <<<<<<<<<<< stuck here, not sure what to put in if, tried grep etc nonthing works yet
times=times+1
fi
current=current+1
done
echo there are $times numbers that has a 5
 
Physics news on Phys.org
  • #2
nvm solved it just going to add this for future people whos going to look up this kind of problem in google

if [[ $current = *[5] ]] ; then
 

1. How do I find numbers with 5 digits in a Unix script?

To find numbers with 5 digits in a Unix script, you can use the grep command with the regular expression pattern "\b[0-9]{5}\b". This will search for numbers with exactly 5 digits in your file or output.

2. Can I use any other regular expressions to find numbers with 5 digits?

Yes, you can use other regular expressions depending on your specific needs. For example, if you want to find numbers with at least 5 digits, you can use the pattern "\b[0-9]{5,}\b". This will find numbers with 5 or more digits.

3. How do I output the numbers with 5 digits to a separate file?

You can use the output redirection operator ">" to redirect the output of your grep command to a separate file. For example, you can use the command "grep "\b[0-9]{5}\b" input.txt > output.txt" to save the numbers with 5 digits from the "input.txt" file to the "output.txt" file.

4. Can I use the sed command to find numbers with 5 digits?

Yes, you can use the sed command with regular expressions to find numbers with 5 digits. For example, you can use the command "sed -n 's/\b[0-9]{5}\b/&/p' input.txt" to print only the numbers with 5 digits from the "input.txt" file.

5. What if my numbers have leading zeros? Will they still be counted as 5 digits?

Yes, if your numbers have leading zeros, they will still be counted as 5 digits. The regular expression pattern "\b[0-9]{5}\b" will match any number with exactly 5 digits, regardless of leading zeros.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
  • Programming and Computer Science
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
Back
Top