Skip to content
Advertisement

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

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

Advertisement