How to grep a line of ps afux filtering by PID [closed] - linux

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I just want to know how to grep a line which consists of for example
pid with number 2.
I want to grep the whole line. Also, it is very important to filter only and exactly "2". Because at the moment It filters all the number which have 2 in it.

If you want to get the listing for just one particular PID, the -p option is the best way.
ps -f -p 2
for example
If you want grep to match a string only if it is the whole word, not part of another word, look at the -w flag, which matches words like
$ echo '52' | grep 2
52
$ echo '52' | grep -w 2
$
if you want to match against only a particular field, awk might be a better answer than grep. For example, if we want to check if the second column is exactly 2 we could do
ps -af | awk '$2 == 2 {print}'

You could go for something like this. If you need the details of a process and you know the pid go for this.
ps afux | awk '{if($2==<pid>) print}'

Related

Cut a string after certain a specific character, but just one field [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is from a vhost file. This is the output I get
ServerName uat3-dam-something1.prg-dc.brb.com
Hello,
I'm wondering how to cut from this output so only this part remains
something1.prg-dc.brb.com
Keep in mind that "something" could be "something4141411" or "something23". So length operations won't work. Tried with cut command and AWK, but didn't work. I would be happy receive a tips from the bash experts :)
Like this :
grep -o 'something.*' file
or more specific:
grep -oE 'something[0-9]+\..*' file
 Output:
something1.prg-dc.brb.com
Could you please try following, written and tested with provided samples only.
awk -F'uat3-dam-' '{print $NF}' Input_file
Description: Making uat3-dam- as field separator and printing last field of it.
2nd solution:
awk 'match($0,/something.*/){print substr($0,RSTART,RLENGTH)}' Input_file
Using:
echo "ServerName uat3-dam-something1.prg-dc.brb.com" |cut -d\- -f3-4
Will return:
something1.prg-dc.brb.com
And if you change the string (as you mention):
echo "ServerName uat3-dam-something111111.prg-dc.brb.com" |cut -d\- -f3-4
It will keep returning:
something111111.prg-dc.brb.com
$ echo 'ServerName uat3-dam-something1.prg-dc.brb.com' | awk -F- '{sub(".*" $2 FS,"")}1'
something1.prg-dc.brb.com
This will work:
echo "ServerName uat3-dam-something1.prg-dc.brb.com" | sed -E 's/.*(something.*)/\1/'
Or, if the string is in a file named file
sed -E 's/.*(something.*)/\1/' file
Explanation:
-E is for extended regex
.*(something.*) means "any char 0 or more times followed by something and any other char 0 or more times".
\1 is used to print only the matching part inside the brackets.
You could also use :
echo ${test#*dam-}
Example :
test="ServerName uat3-dam-something1.prg-dc.brb.com"
echo ${test#*dam-}
which gives:
something1.prg-dc.brb.com
Note that the opposite version would be echo ${test%something*}

how to grep a string from a particular line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to search for a String by navigating to a particular line, How to do this in shell scripting?
For example,
I have
this is the first line
this is the Second line
This is the Third line
Now here i would want to look for string "Third" by going to 3rd line.
Any help is appreciated, Thank you.
Try stringing together cat, sed, and grep.
sed '3!d' filename | grep Third
The unnamed or anonymous pipe (|) and redirection (<, >) are powerful features of many shells. They allow one to combine a set of commands to perform a more complex function.
In the case of this question there were two clear steps,
1) Operate on a specific line of a file (e.g. filter a file)
2) Search the output of the filter for a specific string
Recognizing that there were two steps is a strong indicator that two commands will need to be combined. Therefore, the problem can be solved by finding a solution to each step and then combining them in to one command with pipes and redirection.
If you know about the Stream Editor (sed), it may come to your mind when thinking about how to accomplish the first step of filtering the file. If not searching for, "linux get a specific line of a file" this OS question comes up high in the search results.
$ cat tmp.txt
this is the first line
this is the Second line
This is the Third. line
$ sed '3!d' tmp.txt
This is the Third. line
Knowing that grep can be search for lines with the string of interest the next challenge is to figure out how to get the output of sed as the input to grep. The pipe (|) solves this problem.
sed '3!d' filename | grep Third
Example output:
$ sed '3!d' tmp.txt | grep Third
This is the Third. line
$
Another powerful concept in shell scripting is the exit status. The grep command will set the exit status to 0 when a match is found and 1 when a match is not found. The shell stores the exit status in a special variable named $? (for bash). Therefore, one could use the exit status to conditionally determine the next step in the shell script. The example below does not implement conditions (like if, else). The example below shows the exit status value using the echo command.
$ sed '3!d' tmp.txt | grep Third
This is the Third. line
$ echo $?
0
$ sed '3!d' tmp.txt | grep third
$ echo $?
1
$

If the numbers from 1 to 4321 were written out, how many times would the digit '5' appear? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have figured out the answer for this program, however I don't really understand what does backslush means in here.
I tried to remove the backslush, then the program print all of numbers from 1 to 4321, but it did not grep how many times number 5 appear.
When I put backslush, it just show the number how many time number 5 appear.
here is my code:
#!/bin/bash
seq 4321 | \
while read -n1 digit; do
echo $digit
done | grep -c 5
You can do
$ seq 4321 | tr -Cd 5 | wc -c
1262
which deletes all the chars but 5 and count the result
Usually back slashes when they are put after a command then it means we are telling program that line still continuing and should be considered as a same line commands execution. Usually we do so to make program cleaner(in the way one shouldn't see a LONG line which is keep continuing and shows like a non readable form).
Coming to your code now:
You need not to use \ after seq command since you are using | and while could be mentioned in new line. Moreover your code is not in a long line so it may not be required and should run without it too.
I tested your code without \ and it worked fine for me.
IMHO you need not to use while loop for this task, you could directly do:
seq 4321 | awk '{sum+=gsub(/5/,"&")} END{print sum}'
Try it out if this helps you(if I have got your requirement correctly), this should be faster than a loop and then using grep option.
With GNU grep you can do like this:
$ seq 4321 | grep -o 5 | wc -l
1262
$
According to grep's manual:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

SED Command Replacement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Suppose I have a file with warnings. Each warning in a new line with an id that has 3 capital letters followed by 3 digits only, should be replaced by its id.
Example:
SIM_WARNING[ANA397]: Node q<159> for vector output signal does not exist
The output should be ANA397 and the rest of line is deleted.
How to do so using sed?
I don't think you need sed for that. A simple grep with --only-matching could do, as in:
grep -E 'SIM_WARNING\[(.)\]' --only-matching
should work for you.
Where:
-E does "enhanced regular expressions. I think we need those for capturing with ( )
then follows the pattern, which consists of the fixed SIM_WARNING, followed by a match inside the square brackets
--only-matching simply makes grep print only matching content
In other words: by using ( match ) you tell grep that you only care about the thing in that match pattern.
for id in $(grep -o "^SIM_WARNING\[[A-Z][A-Z][A-Z][0-9][0-9][0-9]\]" test1.bla | grep -o "[A-Z][A-Z][A-Z][0-9][0-9][0-9]" test1.bla ); do echo $id; done
This finds ANA397 from the below.
SIM_WARNING[ANA397]: Node q<159> for vector output signal does not exist
First of all, you have to choose how to use the IDs, for example if you need to cycle the file first or the IDs later...
E.G. (Cycle file first)
exec 3<file
while read -r line <&3; do
id="$(printf "%s" "${line}" | sed -e "s/.*\[\([[:alnum:]]\+\)\].*/\1/")"
### Do something with id
done
exec 3>&-
Otherwise you can decide to cycle the output of sed...
E.G.
for id in $(sed -e "s/.*\[\([[:alnum:]]\+\)\].*/\1/" file); do
### Do something with id
done
Both of the examples should work with posix shell (If I am not missing something...), but shell like posh may not support classes as [[:alnum:]], you can substitute them with the equivalent [a-zA-Z0-9], as every guide will teach you.
Note that the check is not on 3 letters and 3 digits, but for any letter and digit between brackets ([ and ]).
EDIT:
If your lines start with SIM_WARNING you can discriminates those lines with -e "/^SIM_WARNING/! d"
For a strict check on 3 letters and 3 digits you can use -e "s/.*\[\([a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9]\)\].*/\1/"
So taking the example above you can do somethin like:
for id in $(sed -e "/^SIM_WARNING/! d" -e "s/.*\[\([a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9]\)\].*/\1/" file)
### Do something with id
done

unix bash string search without using awk or sed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can't use awk or sed or any other string processing utilities
Any help on this problem is appreciated I have a text file with the following format
BobJanitor20000
TedBuilder30000
NedFighter25000
KitTeacher40000
yes, assume that the names are always three characters long, profession 7 characters and salary 5 characters edit: varaibles come from user input and not as a parameters
I ask the user for a name input and another input whether to display occupation or Salary
If the user enters "Ted" and chooses salary, the output should be
Ted 30000
The program must also take into account partial name matches, "ed" and salary should output
Ted 30000
Ned 25000
I know cut and grep can get me the relavent lines but how do i create the output i want?
cut -c1-3 textFile| grep "$user_input"
gets me the lines i want to use but how do i isolate the Name and profession columns and then the name and salary columns
You must split the input lines in fields first. Something like grep "$user_input" textFile will fail when the input matches a part of the job.
For this reason a simple approach with grep will fail:
With grep you have the option -o to show the matching part only. Combine this with a dot for a single character, and ^ for the beginning of a line or a $ for the end of the line.
# Show salary
# echo "TedBuilder30000" | grep "Ted" | grep -o ".....$"
# Show job
# echo "TedBuilder30000" | grep "Ted" | grep -o "............$" | grep -o "^......."
This will become messy when you want to show the matched name (Ted/Ned) as well.
So how do we split everything up?
I already stored the userinput for name and display in variables. The display is converted in lowercase automaticly with the typeset -l.
userinput=ed
typeset -l display
display=Salary
while read line; do
# offset 0, length 3
name=${line:0:3}
job=${line:3:7}
sal=${line:10:5}
# echo "Debug: Name=$name Job=$job Sal=$sal"
# double brackets for matching the userinput with wildcards
if [[ "$name" = *${userinput}* ]]; then
# Use case for a switch between different possible values
case "${display}" in
"occupation|job") echo "${name} ${job}";;
"salary") echo "${name} ${sal}";;
*) echo "Unsupported display ${display}";;
esac
fi
done < testFile # While reads from testFile, I avoid using cat (Google for uuoc)
You may want:
grep "$user_input" textFile | tee >(cut -c '11-15') | echo `cut -c 1-2`
This is called process substitution.
See https://unix.stackexchange.com/questions/28503/how-can-i-send-stdout-to-multiple-commands
You can use cut with character ranges, for example
echo NedFighter25000 | cut -c 1-3

Resources