How to get a specific word in a file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coaxfiber
    New Member
    • Mar 2007
    • 60

    How to get a specific word in a file?

    hi,

    how can i get the particular line/word in the file.

    Ex:
    $ cat my_file.txt
    1
    2
    3
    4
    5
    6
    7

    I want to get the "7" in the file my_file.txt. Tnx.


    -coax
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    I suppose there is a million ways to do this. The command line:

    cat file.txt | grep "7"

    would "get" the 7, but what do you want to do with the 7 after you "get" it?

    Comment

    • coaxfiber
      New Member
      • Mar 2007
      • 60

      #3
      What I want is to have exactly that specific line?
      Intentionally I used AWK but I want to have my input as my search text.
      this is what exactly what i mean,


      $ Input your name: 7

      And then I want to get that 7 from the file my_file.txt which contains the following inside the file:

      $cat my_file.txt

      102 7 SanFracisco
      102 3 Lasvegas7
      105 5 Nevada7
      106 7 Japan


      If I will going to use "cat file.txt | grep "7" then surely I'll get the following result


      102 7 SanFracisco
      102 3 Lasvegas7
      106 7 Japan


      which means that all lines contain '7' will be the result.

      What I want to be the result:

      102 7 SanFracisco
      106 7 Japan

      only the line which the second word is 7.

      -thanks

      Comment

      • prn
        Recognized Expert Contributor
        • Apr 2007
        • 254

        #4
        How about:

        Code:
        #! /bin/sh
        echo -n "Input your name: "
        read NAME
        echo "Name to find is: $NAME"
        grep -e "\d* $NAME " my_file.txt
        This appears to do what you are looking for. If this file is named "find.sh" and your data is in "my_file.tx t" then

        [prn@deimos ~]$ ./find.sh
        Input your name: 7
        Name to find is: 7
        102 7 SanFracisco
        106 7 Japan

        HTH,
        Paul

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          Originally posted by mac11
          I suppose there is a million ways to do this. The command line:

          cat file.txt | grep "7"

          would "get" the 7, but what do you want to do with the 7 after you "get" it?
          this is Useless use of cat UUOC. grep takes in a file as input
          Code:
          grep "7" inputfile

          Comment

          • coaxfiber
            New Member
            • Mar 2007
            • 60

            #6
            Yeah, "grep -e "\d* $NAME " my_file.txt"


            This will really a big help for my script.
            I tried to 'man grep', i dont usually look for the manual to seek help because sometimes it's more complicated than to ask someone like you.

            Can you tell me what is the purpose of " \d* " in the grep statement you have given to me? Then one more thing again,

            How about when if it is the 3 word in the line I want to grep.
            Now I need the 8. I think after this I could use grep better.

            102 7 8 SanFracisco
            102 3 8 Lasvegas7
            105 5 5 Nevada7
            106 7 2 Japan

            > 102 7 8 SanFracisco
            > 102 3 8 Lasvegas7

            I wish I give sense to this. Thank You Very Much.

            -coax

            Comment

            • ghostdog74
              Recognized Expert Contributor
              • Apr 2006
              • 511

              #7
              Originally posted by coaxfiber
              What I want is to have exactly that specific line?
              Intentionally I used AWK but I want to have my input as my search text.
              this is what exactly what i mean,


              $ Input your name: 7

              And then I want to get that 7 from the file my_file.txt which contains the following inside the file:

              $cat my_file.txt

              102 7 SanFracisco
              102 3 Lasvegas7
              105 5 Nevada7
              106 7 Japan


              If I will going to use "cat file.txt | grep "7" then surely I'll get the following result


              102 7 SanFracisco
              102 3 Lasvegas7
              106 7 Japan


              which means that all lines contain '7' will be the result.

              What I want to be the result:

              102 7 SanFracisco
              106 7 Japan

              only the line which the second word is 7.

              -thanks
              it can be done in awk.
              Code:
              awk '$2 == 7 {print }' "file"  #or awk '$2 == 7' "file"

              Comment

              • ghostdog74
                Recognized Expert Contributor
                • Apr 2006
                • 511

                #8
                Originally posted by coaxfiber
                Yeah, "grep -e "\d* $NAME " my_file.txt"


                This will really a big help for my script.
                I tried to 'man grep', i dont usually look for the manual to seek help because sometimes it's more complicated than to ask someone like you.

                Can you tell me what is the purpose of " \d* " in the grep statement you have given to me? Then one more thing again,

                How about when if it is the 3 word in the line I want to grep.
                Now I need the 8. I think after this I could use grep better.

                102 7 8 SanFracisco
                102 3 8 Lasvegas7
                105 5 5 Nevada7
                106 7 2 Japan

                > 102 7 8 SanFracisco
                > 102 3 8 Lasvegas7

                I wish I give sense to this. Thank You Very Much.

                -coax
                also in awk:
                Code:
                awk '$2 == 7 && $3 == 8 {print }' "file" # or awk '$2 == 7 && $3 == 8' "file"

                Comment

                • coaxfiber
                  New Member
                  • Mar 2007
                  • 60

                  #9
                  Originally posted by ghostdog74
                  also in awk:
                  Code:
                  awk '$2 == 7 && $3 == 8 {print }' "file" # or awk '$2 == 7 && $3 == 8' "file"
                  Hi.. I think I almost found exactly what will really answer my question.. What if the third word is not always '8'. How about it will depend on my input?
                  $Input ID: 6
                  then it will search for all lines that contain 6 in the 3rd word. Thanks.

                  Comment

                  • prn
                    Recognized Expert Contributor
                    • Apr 2007
                    • 254

                    #10
                    Originally posted by coaxfiber
                    Can you tell me what is the purpose of " \d* " in the grep statement you have given to me? Then one more thing again,

                    How about when if it is the 3 word in the line I want to grep.
                    Now I need the 8. I think after this I could use grep better.
                    "\d* " means an arbitrary number of digits (\d) followed by a space.

                    As for the question of "8" as the third word, I might suggest you rethink the whole premise of the question.

                    The more different things you want your script to do, the more you need to write with that in mind from the beginning. You have now asked for 3 pretty different criteria (just search for "7", "7" as the second "word", and now "8" as the third "word"). If you want something so general that it can do any one or all of these, then you need to decide from the beginning what information you need, how you want to interpret that information and then what you want to do with it. I don't think it's very profitable for any of us to answer a question and then be asked again: "OK, but what if ...?"

                    Take a short break and think about what you are really looking for. Ghostdog74 is probably on the right track if you want to manipulate "words" on lines. awk is a good choice for that. If you're going off in some other direction, then awk may (or may not) turn out to be best for that. So far, we're all just proposing answers based on the couple of examples you give us at a time. Think about what your real data might look like and what you really want to extract from it.

                    Best Regards,
                    Paul

                    Comment

                    • coaxfiber
                      New Member
                      • Mar 2007
                      • 60

                      #11
                      Yes, I agree to you.I want to give some apology if I had to overuse this forum for my own benefit. Perhaps I need to concentrate on my work. I hope I could also help some people who also needs some support.. Thanks.

                      Comment

                      • ghostdog74
                        Recognized Expert Contributor
                        • Apr 2006
                        • 511

                        #12
                        Originally posted by coaxfiber
                        Hi.. I think I almost found exactly what will really answer my question.. What if the third word is not always '8'. How about it will depend on my input?
                        $Input ID: 6
                        then it will search for all lines that contain 6 in the 3rd word. Thanks.
                        then you can do this for example
                        Code:
                        echo "Enter input"
                        read input
                        awk -v input=$input '$2 == 7 && $3 == input {print }' "file"

                        Comment

                        • coaxfiber
                          New Member
                          • Mar 2007
                          • 60

                          #13
                          Originally posted by ghostdog74
                          then you can do this for example
                          Code:
                          echo "Enter input"
                          read input
                          awk -v input=$input '$2 == 7 && $3 == input {print }' "file"
                          Thank You Sir! This is exactly what I need. I had tried to used the ' -v ' option before but some syntax came to error.

                          BR,
                          -coax

                          Comment

                          • marimuthup
                            New Member
                            • Aug 2007
                            • 1

                            #14
                            with awk or grep how would I get a specific word in a file, rather than a lin.

                            For example:

                            File contains lines such as

                            This is MyTime:00:0450 Secs

                            I wanna to extract the String where it is between 'MyTime:' and Secs, where I need only the String '00:0450' printed.

                            Definelty not the entire row.

                            Could some one help me?

                            Comment

                            • docdiesel
                              Recognized Expert Contributor
                              • Aug 2007
                              • 297

                              #15
                              Originally posted by marimuthup
                              with awk or grep how would I get a specific word in a file, rather than a lin. For example:

                              File contains lines such as
                              This is MyTime:00:0450 Secs
                              ....
                              Hi,

                              perhaps you should've opend a new thread for your question. Anyway, what may fit your needs is a line like this:

                              Code:
                               # read from file, replace 1st ':' with space, then print column #4 only
                               sed <inputfile  s/:/\ /  |  awk '{print $4;}'
                              Regards,

                              Bernd

                              Comment

                              Working...