I want to use "awk" or sed to print all the lines that start with "comm=" in a file
25,659
Solution 1
For lines that start with comm=
sed -n '/^comm=/p' filex
awk '/^comm=/' filex
If comm=
is anywhere in the line then
sed -n '/comm=/p' filex
awk '/comm=/' filex
Solution 2
You could use grep also :
grep comm= filex
this will display all the lines containing comm=
.
Solution 3
Here's an approach using grep:
grep -o '\<comm=[[:alnum:]]*\>'
This treats a word as consisting of alphanumeric characters; extend the character class as needed.
Author by
wael
Updated on July 09, 2022Comments
-
wael almost 2 years
I want to use "awk" or "sed" to print all the lines that start with
comm=
from the filefilex
, Note that each line contains "comm=somthing"for example : comm=rm , comm=ll, comm=ls ....
How can i achieve that ?
-
wael over 12 yearsthat only works it comm is at the beginning of the line, how to make it work if comm is at any place in each line, thanks for ur help Jaypa
-
Michael J. Barber over 12 years@bob Given that you've stated in a comment that it's just one
comm=
per line, this solution is overkill; use the answer from Cédric Julien. Additionally, to say "thanks" on Stackoverflow, you can just upvote the useful answers rather than commenting. -
wael over 12 yearsthis is returning the whole line i just want to return the match, and this is how it can be done : grep -o '\<comm=[[:alnum:]]*\>' , thanks for your help
-
potong over 12 yearsFor brevity you might like
sed '/\<comm=/!d'
-
vefthym about 10 yearsor
grep "^comm=" filex
to match lines starting withcomm=
?