Hello there,
Suppose i have this huge dictionary .txt in my system. Now i need to write a shell script that takes a numeric input from the user( $1 ), the number the user input is supposed to specify the lenght of the word the user is looking for in the dictionary txt.
In other words the output should look like this:
$./myegrep 3
aim
aaa
etc
Ian
Lan
Net
$./myegrep 5
Arthur
Crave
Grave
Since
Got it? Also i am trying to do this without using any kind of loops/if statements whatsoever. It should be a simple egrep one line with a regular expression, but i am having trouble figuring out how to implement the user input into the regular expression. Any help? Thanks in advance!
How to find specific word by lenght from specific file?
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
How to find specific word by lenght from specific file?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: How to find specific word by lenght from specific file?
would be helpful if you showed us your script, as "Arthur" is 6 characters long.
Re: How to find specific word by lenght from specific file?
I've put your lines into a file called testfile. Here's example of using egrep to get lines 3, 5 or 6 characters long:
In a script you could use:
Pass this script the length you are looking for and it will return the lines from testfile that match that length.
Edit: to get better at bash go read these two, highly recommended:
- http://mywiki.wooledge.org/BashGuide
- http://mywiki.wooledge.org/BashFAQ/
Code: Select all
$ egrep ^.{3}$ testfile
aim
aaa
etc
Ian
Lan
Net
$ egrep ^.{5}$ testfile
Crave
Grave
Since
$ egrep ^.{6}$ testfile
Arthur
Code: Select all
#!/usr/bin/env bash
egrep ^.{$1}$ testfile
Edit: to get better at bash go read these two, highly recommended:
- http://mywiki.wooledge.org/BashGuide
- http://mywiki.wooledge.org/BashFAQ/