I'm working on a text file that contains normal text with LaTeX-style comments (lines starting with a %). To determine the non-comment word count of the file, I was running this command in Bash:
grep -v "^%" filename | wc -w
which returns about the number of words I would expect. However, if from within vim I run this command:
:r! grep -v "^%" filename | wc -w
It outputs the word count which includes the comments, but I cannot figure out why.
For example, with this file:
%This is a comment.
This is not a comment.
Running the command from outside vim returns 5, but opening the file in vim and running the similar command prints 9.
I also was having issues getting vim to prepend a "%" to the command's output, but if the output is wrong anyways, that issue becomes irrelevant.
The % character is special in vi. It gets substituted for the filename of the current file.
Try this:
:r! grep -v "^\%" filename | wc -w
Same as before but backslash-escaping the %. In my testing just now, your example :r! command printed 9 as it did for you, and the above printed 5.
Related
I have a script located at /usr/local/bin/gq which is returned by the command whereis gq, well almost. What is actually returned is gq: /usr/local/bin/gq. But the following gives me just the filepath (with some white space)
whereis gq | cut -d ":" -f 2
What I’d like to do is be able to pipe that into cat, so I can see the contents. However the old pipe isn’t working. Any suggestions?
If you want to cat the contents of gq, then how about:
cat $(which gq)
The command which gq will result in /usr/local/bin/gq, and the cat command will act on that.
in applescript editor:
do shell script "grep -w 'SomeText' /tmp/test"
ignores -w
in Bash:
grep -w 'SomeText' /tmp/test
not ignores arguments
But for example arguments -v (negative) works in AppleScript with do shell script
it is happening on both different computers with different systems
how i can use -w argument in grep from applescript?
Thanks!
Regardless of where I run the grep -w ... command from, Terminal or ApplesScript's do shell script command, I get identical output.
The manual page for the -w option in grep states the following:
−w, −−word-regexp
The expression is searched for as a word (as if surrounded by ‘[[:<:]]’ and ‘[[:>:]]’; see re_format(7)).
The manual page for re_format states:
There are two special cases‡ of bracket expressions: the bracket expressions [[:<:]] and [[:>:]] match the null string at the beginning and end of a word respectively. A word is defined as a sequence of word characters which is neither preceded nor followed by word characters. A word character is an alnum character (as defined by ctype(3)) or an underscore.
In Terminal:
Contents of /tmp/test:
$ cat /tmp/test
SomeText
MoreText
ASomeTextZ
Other Text
0 SomeText 1
$
Using grep without -w on /tmp/test:
$ grep 'SomeText' /tmp/test
SomeText
ASomeTextZ
0 SomeText 1
$
As it should, grep finds all three lines containing 'SomeText'.
Using grep with -w on /tmp/test:
$ grep -w 'SomeText' /tmp/test
SomeText
0 SomeText 1
$
As it should, grep -w finds only the lines conforming to what's stated in the manual page excerpts shown above. In this case, only two of the three lines that contain 'SomeText'.
The output of each grep command, show above, when wrapped in a do shell script command in AppleScript are identical, as should be.
In Script Editor:
Because these are the expected results is why I'm adamant about following How to create a Minimal, Complete,and Verifiable example, when asking questions such that you have, in the manner you have!
I'd suggest you show us the actual content of your /tmp/test file and the actual output you get from each of the grep commands, with and without the -w option, from both Terminal and AppleScript's do shell script command.
Although it shouldn't make a difference, nonetheless you should also provide macOS version info so we can test this under the actual version of macOS you're using, so as to see if that's a relevant factor in the equation.
I have this particular file with special characters:
^S^H^B^R^MLong is here
^X^M
When I search for it via grep -r "Long is here" . it will give me the following as result, not revealing its file path:
Long is here
However, if I remove the ^S^H^B^R^M, the file path will show up:
./CIQla:Long is here
I'm using OSX.
What's going on here?
EDIT:
I tried it with both BSD grep (grep 2.5.1-FreeBSD) as GNU grep (ggrep 2.26)
The ^M is a carriage return, which makes the terminal return to the beginning of the line. Essentially, it prints "./CIQla:", then several nonprinting characters, then ^M sends it to the beginning of the line, then it prints "Long is here" over the "./CIQla:".
If you want to be able to see the filename in the output, you need to pipe the result through something that'll make the ^M (and probably the other nonprinting characters) into something printable. Try these:
grep -r "Long is here" . | cat -v
grep -r "Long is here" . | more
New to UNIX, currently learning UNIX via secureshell in a class. We've been given a few basic assignments such as creating loops and finding files. Our last assignment asked us to
write code that will estimate the number of shell scripts in the current directory and then print out that total number as "Estimated number of shell script files in this directory:"
Unlike in our previous assignments we are now allowed to use conditional loops, we are encouraged to use grep and wc statements.
On a basic level I know I can enter
ls * .sh
to find all shell scripts in the current directory. Unfortunately, this doesn't estimate the total number or use grep. Hence my question, I imagine he wants us to go
grep -f .sh (or something)
but I'm not exactly sure if I am on the right path and would greatly appreciate any help.
Thank You
You can do it like:
echo "Estimated number of shell script files in this directory:" `ls *.sh | wc -l`
I'd do it this way:
find . -executable -execdir file {} + | egrep '\.sh: | Bourne| bash' | wc -l
Find all files in the current directory (.) which are executable.
For each file, run the file(1) command, which tries to guess what type of file it is (not perfect).
Grep for known patterns: filenames ending with .sh, or file types containing "Bourne" or "bash".
Count lines.
Huhu, there's a trap, .sh file are not always shell script as the extension is not mandatory.
What tells you this is a shell script will be the Shebang #!/bin/*sh ( I put a * as it could be bash, csh, tcsh, zsh, which are shells) at top of line, hence the hint to use grep, so the best answer would be:
grep '^#!/bin/.*sh' * | wc -l
This give output:
sensible-pager:#!/bin/sh
service:#!/bin/sh
shelltest:#!/bin/bash
smbtar:#!/bin/sh
grep works with regular expression by default, so the match #!/bin/.*sh will match files with a line starting (the ^) by #!/bin/ followed by 0 or unlimited characters .* followed by sh
You may test regex and get explanation of them on http://regex101.com
Piping the result to wc -l to get the number of files containing this.
To display the result, backticks or $() in an echo line is ok.
grep -l <string> *
will return a list of all files that contain in the current directory. Pipe that output into wc -l and you have your answer.
Easiest way:
ls | grep .sh > tmp
wc tmp
That will print the number of lines, bytes and charcters of 'tmp' file. But in 'tmp' there's a line for each *.sh file in your working directory. So the number of lines will give an estimated number of shell scripts you have.
wc tmp | awk '{print $1}' # Using awk to filter that output like...
wc -l tmp # Which it returns the number of lines follow by the name of file
But as many people say, the only certain way to know a file is a shell script is by taking a look at the first line an see if there is #!/bin/bash. If you wanna develop it that way, keep in mind:
cat possible_script.x | head -n1 # That will give you the first line.
I'll try and make this question short. Basically, I am working on a shell script, and I have a .plist file containing an integer value that I am trying to "extract" and put into a variable in my shell script.
I'm able to refine the contents of the .plist file to a few lines, but I am still getting a bunch of characters I don't need.
I am delcaring / running the following command in my shell script, and it is giving me the following results.
file_refine=`grep -C 2 CFBundleVersion $file | grep '[0-9]\{3\}'`
Output
<string>645</string>
I just need the numeral digits not the string tags, but I can't seem to figure that out.
Try this
file_refine=$(grep -C 2 CFBundleVersion $file | grep -o '[0-9]\{3\}')
the -o option from grep man page:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with
each such part on a separate output line.