BASH scripting to find one word sentences
given the name of a file as an argument,the script reads the file name and creates a new file containing only lines which consist of one word.
An example input and output will be
There are 20 stude开发者_开发问答nts in the class. [TAB][SPACE] Nearly half of them are enrolled in FoS. The rest are in Faculty-Of-ES.
The output from the script should look like
There [TAB][SPACE] Nearly Faculty-Of-ES.
Hello I am a beginner programmer and I am trying to learn bash scripting for over a month now but questions like this still stump me. Please outline the general approach .Thank you
grep -P "^[\s]*[0-9A-Za-z-]+.?[\s]*$" test.txt
Where test.txt
contains the input text.
Here I am defining a word as having zero or more whitespace in the beginning and end . Then any combination of alphabets, digits and -
and optionally ending with a period.
NOTE :: This will not work if you are considering floating point numbers as a word. Hence your definition of a word
defines the result and the regex.
probably the simplest way: awk 'NF==1' filename
Code: cat $1 | egrep "^\s*\w+\s*$" > output.txt
.
Explanations:
- You can reference the variables your program get using
$1..$9
so if you run your program$ > myprogram filename.txt
then$1
will befilename.txt
2.You can read a file's content using cat filename
. You can pipe the files content to the next line of code using |
(pipe symbol).
You can use the
egrep
program to only show lines where a regular expression matches.a regular expression to get only only one word lines might be:
^\s*\w+\s*$
. which reads: there can be zero or more spaces at the start of the line (\s - spaces, * is zero or more, ^ is the start of the line). then one or more characters (\w is letters, digits and underline, + is one or more). then zero or more spaces and then must be the end of the line. meaning this regular expression won't show the line 'something something' because it expects the end of the line after the space, not more words.You can redirect the output of egrep to a file using redirections:
> filename
.
精彩评论