Announcement

Collapse
No announcement yet.

grep

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    grep

    grep command is pretty neat-o. 8) But I find it lacking :P

    Code:
    ant2ne@ant2ne-laptop:~$ ps -A | grep "konsole"
    15400 ?    00:00:00 konsole
    ant2ne@ant2ne-laptop:~$
    What I want to do is
    ant2ne@ant2ne-laptop:~$ ps -A | grep "konsole" > kill
    But the other information after the PID (ie. "? 00:00:00 konsole" ) is ruining my kill command.

    How can I do this in a simple script without breaking out the gcc. (I haven't rented the C book from the library yet.)

    (note: this is just an example. I do have other uses for the kill and grep commands. I do have other ways of killing the konsole i'm typing into)
    Registered Linux User: 450747<br />Registered Ubuntu User: 16269

    #2
    Re: grep

    One option (by far not the only one) is to use the 'cut' command after grep, see 'man cut'

    crude example:

    command | grep (to get the correct line) | cut (to get the correct part of the line)

    Comment


      #3
      Re: grep

      not sure your redirecting the output of ps|grep to kill will work.
      also, doing ps|grep sometimes results in 2 lines, one of which is the grep itself.
      you gotta get rid of that, too.
      anyway, an alternative to cut is awk
      i'd do it like this:
      Code:
      kill `ps -A | grep konsole | grep -v grep | awk '{print $1}'`
      hth
      gnu/linux is not windoze

      Comment


        #4
        Re: grep

        I'm sure I don't get it, but why wouldn't you just

        sudo killall konsole

        Comment


          #5
          Re: grep

          Originally posted by lingenfr
          I'm sure I don't get it, but why wouldn't you just

          sudo killall konsole
          Like I said above, I do have other ways of killing it. But I want to figure out how to get pieces of info off of a line with the grep command.

          Originally posted by jankushka
          Code:
          kill `ps -A | grep konsole | grep -v grep | awk '{print $1}'`
          That worked.. now I just gotta figure out "how that worked" I never seen 'grep -v grep" or "awk" much less '{print $1}'` I have no idea what those do. But it is interesting to know, that the things I want to do ARE possible.
          Registered Linux User: 450747<br />Registered Ubuntu User: 16269

          Comment


            #6
            Re: grep

            grep -v grep
            http://linux.die.net/man/1/grep

            awk '{print $1}'
            http://www.tldp.org/LDP/abs/html/awk.html

            Comment

            Working...
            X