Visit The New, Modern Unix Linux Community


Basic grep question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Basic grep question
# 1  
Basic grep question

Shell : Bash shell (Fedora 26)

In the below text file (output.txt), I need to find all lines starting with the pattern pc. As you can see, only one line matches this condition (pc hello world).

But, my below 3 attempts return wrong output. How do I use the grep command correctly here ?


Code:
 # cat output.txt
  sunrpc                331776  1
  crct10dif_pclmul       16384  0
  crc32_pclmul           16384  0
  shpchp                 36864  0
  snd_pcm               102400  4 snd_hda_intel,snd_hda_codec,snd_hda_core,snd_hda_codec_hdmi
  snd_timer              32768  2 snd_seq,snd_pcm
  lpc_ich                24576  0
  pc hello world

  # Attempt1
  # It returns any line with the the pattern pc in it which is not what I want
  # grep 'pc' output.txt
  sunrpc                331776  1
  crct10dif_pclmul       16384  0
  crc32_pclmul           16384  0
  shpchp                 36864  0
  snd_pcm               102400  4 snd_hda_intel,snd_hda_codec,snd_hda_core,snd_hda_codec_hdmi
  snd_timer              32768  2 snd_seq,snd_pcm
  lpc_ich                24576  0
  pc hello world
  # 


  # Attempt2
  # No output returned at all
  # grep 'pc^' output.txt



  # Attempt3 
  # I think this gives the same wrong output as Attempt1 
  # grep 'pc*' output.txt
  sunrpc                331776  1
  crct10dif_pclmul       16384  0
  crc32_pclmul           16384  0
  shpchp                 36864  0
  snd_pcm               102400  4 snd_hda_intel,snd_hda_codec,snd_hda_core,snd_hda_codec_hdmi
  snd_timer              32768  2 snd_seq,snd_pcm
  lpc_ich                24576  0
  pc hello world
  #

# 2  
Normally you would use:
Code:
grep '^pc'

I do not know if the leading spaces are present in the actual file. In that case you would need something like:
Code:
grep '^[[:blank:]]*pc'



--
Note: on Solaris use /usr/xpg4/bin/grep
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Print if the first word is pc (white space delimited)
Code:
awk '$1=="pc"'

This User Gave Thanks to MadeInGermany For This Post:
# 4  
You could be right Scrutinizer. All those lines except pc hello world comes from lsmod command in Linux. It could have leading spaces.

Hi MadeInGermany,
What does the 2 equal operators == in your awk solution do ?
# 5  
In awk the == operator compares two numbers or strings, in this case strings.
If they are equal the result is "true", a non-zero. The default action for a non-zero is {print}.
You can write this more explicit
Code:
awk '$1=="pc" { print }'

or
Code:
awk '{ if ($1=="pc") { print } }'

or even
Code:
awk '{ if ($1=="pc") { print $0 } }'

This User Gave Thanks to MadeInGermany For This Post:

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #78
Difficulty: Easy
Linux was inspired by MINIX
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic grep -c Question

Hello, I have a csv list which contains a reference and tariff code for every customer we have (~7.2 million) looking something like this: customernumber,tariff 012345678910,T0001 012345678911,T0002 012345678912,T0001-A0001 I have a 2nd list which contains every unique tariff and... (8 Replies)
Discussion started by: Cludgie
8 Replies

2. Shell Programming and Scripting

Basic Grep

I am trying to grep a section of .txt file...but once I grep the certain area of the file I would like to display all lines below it as well....how do I have to go about doing this... example grep "Sidney Crosby" hockey.txt result Sidney Crosby Age Goals Assist Can this be done... (8 Replies)
Discussion started by: chrisp200
8 Replies

3. Shell Programming and Scripting

basic grep question

I think I am doing this correctly, but it is responding very quickly with no results so I am not sure. I need to do a case insensitive grep of all files in my current directory grep -i <keyword> /my/directory is that correct? (1 Reply)
Discussion started by: guessingo
1 Replies

4. Shell Programming and Scripting

basic nc question

i'm doing this in one terminal: nc -lu 7402 and it appears to start listening properly, then in another i do this: echo "hello" | nc -u localhost 7402 and nothing happens on the listening terminal - what am i doing wrong? thanks. (7 Replies)
Discussion started by: peterworth
7 Replies

5. Shell Programming and Scripting

basic question

hi, I have a basic question,, i am in a directory called /intas/OCU_3.9.1/sbin ocuut1@france>mv itsa_tcs itsa_tcs_old mv: itsa_tcs_old: rename: Permission denied i am logging as the owner of the file. when i am doing this i am getting the above error of permission denied. I know... (3 Replies)
Discussion started by: namishtiwari
3 Replies

6. HP-UX

Basic OS question

Could someone tell me the command to find out the OS version which will give 12 character not the 9 characters(which is usually machine id). uname -i gives machine id and uname -a is more comprehensive way to look. Thanks! (4 Replies)
Discussion started by: catwomen
4 Replies

7. UNIX for Dummies Questions & Answers

Really basic question....

Hello all. Let me start off by saying I know a little more then it seems by me asking this question... here goes I have an old 486 box and I want to start messing around with unix. I've been taking classes for 3 or 4 years in c programming in unix, so I am used to the commands and such, but I... (1 Reply)
Discussion started by: robherms
1 Replies

8. UNIX for Dummies Questions & Answers

Very Basic Question

How to know if my AIX 5.2 is running at 64bits? THANKS (5 Replies)
Discussion started by: GermanSkull
5 Replies

9. UNIX for Dummies Questions & Answers

basic question

hey...when i type who...what does "pts" field mean??? eg pts 0 etc (1 Reply)
Discussion started by: urwannabefriend
1 Replies

10. UNIX for Dummies Questions & Answers

basic question

I have some basic doubts. Can someone clarify in this forum? 1)if then eval ' tset -s -Q -m ':?hp' ' else eval ' tset -s -Q ' what does it exactly mean in .profile? 2) what are 'nobody' and 'noaccess' usernames in /etc/passwd file. ... (3 Replies)
Discussion started by: asutoshch
3 Replies

Featured Tech Videos