I want to use bash to grep if any process is listening on port 80,but now I stumble upon other port number which annoys me. How can I filter this in grep? It also outputs other records such as 8080 8088 and so on. Answer I’m looking at the output of my netstat command, and see this: I take it
Tag: grep
BASH – weird characters coming out from a pipe
I have this script which simply kills an app If I write the kill command manually as a oneliner than it works. But I have to run it as a script which doesn’t work. So I tried to debug it and here is the result Why do I keep getting those strange characters on the kill line? How can i
Use grep to match a pattern in a line only once
I have this: Which gives this: I understand whats happening. How do I stop grep from trying to continue matching after 1 successful match? How do I get only Answer You need to do the grouping: (…) followed by the exact number of occurrence: {<n>} to do the job: Hope it helps. Cheers!!
How to make GREP select only numeric values?
I use the df command in a bash script: This script returns: But I need only numbers (to make the next comparison). If I use the grep regex without the dot: I receive nothing. How to fix? Answer If you try: It returns: Here’s the details on the -o (or –only-matching flag) works from the grep manual page. Print only
sed replace with hex
UTF-8 file test.txt: hex is sed s/A/B/g test.txt works sed s/x41/B/g test.txt does not work Some characters are unprintable so I must use their hex, A is just an example. Answer the shell preprocesses it, use single quotes.
How do I add a line of text to the middle of a file using bash?
I’m trying to add a line of text to the middle of a text file in a bash script. Specifically I’m trying add a nameserver to my /etc/resolv.conf file. As it stands, resolv.conf looks like this: My goal is to add nameserver 127.0.0.1 above all other nameserver lines, but below any text above that. In the end I want to
How can I exclude directories from grep -R?
I want to traverse all subdirectories, except the “node_modules” directory. Answer SOLUTION 1 (combine find and grep) The purpose of this solution is not to deal with grep performance but to show a portable solution : should also work with busybox or GNU version older than 2.5. Use find, for excluding directories foo and bar : Then combine find and
How to specify ignore pattern in grep?
How can I make grep to ignore some files (e.g. .svn directories, binary files, etc.) while grepping recursively? Answer Use an –exclude pattern as already answered here
How can I format my grep output to show line numbers at the end of the line, and also the hit count?
I’m using grep to match string in a file. Here is an example file: grep -i null myfile.txt returns How can I return matched lines together with their line numbers like this: I know -c returns total matched lines, but I don’t how to format it properly to add total null count in front, and I don’t know how to
Solaris equivalent of -o option of grep on Linux
I run the following on Sun Solaris — it runs OK on Linux but not on Sun Solaris: My question is which option on Sun Solaris does the same task as the option grep -o (to match string capture) on Linux? Answer Solaris grep doesn’t seem to have such an option. If you just need this to run on some Solaris