Append to a file list of processes that match a certain pattern - linux

This is the exact question: Append to a file in your home directory called 'water.txt' a list of all processes that have the string 'er' at the end of their name.
I know the command to list running process are ps -A, or top but the hard part is the appending only certain processes to a new file based on pattern match
The two commands that come to mind are cut and grep but I don't know exactly how to combine them together especially because the list of processes are not stored in a file/ or are they?

A command that is the combination of ps and grep is called pgrep.
With that command, you can do this to list all files that end in er:
pgrep -fa 'er$'
The option '-f' is to use the "full" name of the commands, and '-a' is to list the full name of the command with the PID number.
And to redirect the output to a file, just use '>':
pgrep -f 'er$' > ~/water.txt
The ~ means to use your home directory.

If you're looking to append all processes ending with the string er, you should use a mixture of ps and grep:
ps -aux | grep 'er$' >> ~/water.txt
The $ at the end of the er string is to make sure the process finishes with those 2 characters.

Related

List each file that doesn't match a pattern recursively

Tried the following command, it lists all the lines including file names
which are not matching the given pattern.
grep -nrv "^type.* = .*"
"But what we need is list of file names in a folder with content
which does not have even a single occurrence of above pattern."
Your help will be really appreciated.
You need the -L option:
grep -rL '^type.* = .*' directory_name
From the GNU grep manual:
-L, - -files-without-match
    Suppress normal output; instead print the name of each input file from which no output    would normally have been printed. The scanning will stop on the first match.

linux script shell : grep a part of path in a list of path

In my script shell, i have 2 files. The first one is a file containing only names of files with part of the path :
list1:
aaa/bbb/file1.ext
ccc/ddd/file2.ext
eee/fff/file3.ext
The second one is a list of every files of the extension ".ext" with the absolute path before them:
list2:
/home/.../aaa/bbb/file1.ext
...
...
...
/home/...ccc/ddd/file2.ext
...
And I am trying to extract the lines of the second file list2, containing the lines of the first one with grep.
For now I tried :
while read line
do
grep "$line" "list1"
done < list2
But this command doesn't ouptut anything, however the command
grep "aaa/bbb/file1.ext" "list1"
have the output I am waiting for
/home/.../aaa/bbb/file1.ext
Anyone sees what I am missing on this script? Thanks
This is one of the cases where -f option from grep comes very handy:
grep -f f1 f2
For your given input returns:
/home/.../aaa/bbb/file1.ext
/home/...ccc/ddd/file2.ext
From man grep:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty file contains zero
patterns, and therefore matches nothing. (-f is specified by
POSIX.)

How do I use the filenames output by "grep" as argument to another program

I have this grep command which outputs the names of files (which contains matches to some pattern), and I want to parse those files with some file-parsing program. The pipechain looks like this:
grep -rl "{some-pattern}" . | {some-file-parsing-program} > a.out
How do I get those file names as command line arguments to the file-parsing program?
For example, let's say grep returns the filenames a, b, c. How do I pass the filenames so that it's as if I'm executing
{some-file-parsing-program} a b c > a.out
?
It looks to me as though you're wanting xargs:
grep -rl "{some_pattern" . | xargs your-command > a.out
I'm not convinced a.out is a good output file name, but we can let that slide. The xargs command reads white-space separated file names from standard input and then invokes your-command with those names as arguments. It may need to invoke your-command several times; unless you're using GNU xargs and you specify -r, your-command will be invoked at least once, even if there are no matching file names.
Without using xargs, you could not use sed for this job. Without using xargs, using awk would be clumsy. Perl (and Python) could manage it 'trivially'; it would be easy to write the code to read file names from standard input and then process each file in turn.
I don't know of any linux programs that cannot read from stdin. Depending on the program, the default input may be stdin or you may need to specify to use stdin by using a command line option (often - by itself). Do you have anything particular in mind?

cannot understand grep in cron job solution

i was looking at the solution of Run cron job only if it isn't already running in order to apply it to a similar problem that I have but I cannot understand the
ps -u $USER -f | grep "[ ]$(cat ${PIDFILE})[ ]"'
It appears to be saying check the end of each line from ps for ' PIDnumber ' but when I look at my ps output the PIDnumber is in column two. I am interpreting the first $ as the regular expression check_end_of_line option.
$(stuff) will execute "stuff" (in this case cat ${PIDFILE})
PIDFILE is assumed to be a path to a file, so the whole line is basically looking for any line in the ps output that contains the contents of the "pid file" ([ ] adds some spaces on each side of the pid so that if pid file contains '888' it wont match '8888' in the ps output)

Terminal command to find lines containing a specific word?

I was just wondering what command i need to put into the terminal to read a text file, eliminate all lines that do not contain a certain keyword, and then print those lines onto a new file. for example, the keyword is "system". I want to be able to print all lines that contain system onto a new separate file. Thanks
grep is your friend.
For example, you can do:
grep system <filename> > systemlines.out
man grep and you can get additional useful info as well (ex: line numbers, 1+ lines prior, 1+lines after, negation - ie: all lines that do not contain grep, etc...)
If you are running Windows, you can either install cygwin or you can find a win32 binary for grep as well.
grep '\<system\>'
Will search for lines that contain the word system, and not system as a substring.
below grep command will solve ur problem
grep -i yourword filename1 > filename2
with -i for case insensitiveness
without -i for case sensitiveness
to learn how grep works on ur server ,refer to man page on ur server by the following command
man grep
grep "system" filename > new-filename
You might want to make it a bit cleverer to not include lines with words like "dysystemic", but it's a good place to start.

Resources