Extract values from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daku21
    New Member
    • Jul 2014
    • 2

    Extract values from a file

    Hello,
    Can somebody help me on the below.
    i am trying to write a bash script in my linux red hat system in order to do the follow
    i have the below text file, lets say "file1.txt" and i want my script (script1.sh) to extract two specific values, and these two values to be used in another script (script2.sh)

    file1.txt
    text 1 in to use............ ............... ...15243
    text 2 in to use............ ............... ...A
    text 3 in to use............ ............... ...OR
    text 4 in to use............ ............... ...UNLL
    text 5 in to use............ ............... ...1
    text 6 in to use............ ............... ...N
    text 7 in to use............ ............... ...00BN/12345D
    text 8 in to use............ ............... ...ABC
    text 9 in to use............ ............... ...11ADF/123E
    text 10 in to use............ ............... ..N
    text 11 in to use............ ............... ..N
    text 15 in to use............ ............... ..07/12
    text 16 in to use............ ............... ..17-96
    text 17 in to use............ ............... ..Y

    how to use script1.sh to read file1.txt and get in the 7th line the value "12345" with out "D", and in the 9th line the "123" value with out "E".
    these two values i need to use in script2.sh

    thank you,
    daku
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    I hate to say, but this sounds a lot like homework. That said, I will guide you, but not write this for you.

    You will want to start with a bash tutorial. You can google and there are many out there. http://goo.gl/RyL9mS

    If you want to post code, we can help guide you through it. Just post the code (in code quotes) and provide us the error(s) you are seeing and what is expected.

    Regards,

    Jeff

    Comment

    • Luuk
      Recognized Expert Top Contributor
      • Mar 2012
      • 1043

      #3
      another hint:
      awk (or gawk) seems a tool which could be usefull

      Because:
      1) i know how to solve this with gawk
      2) read the man-pages for gawk and decide for yourself ;)

      Comment

      • daku21
        New Member
        • Jul 2014
        • 2

        #4
        hello,
        i created a one line bash script but i am not good in programming/linux.

        #!/bin/bash
        awk '/text 7 in to use'/ file1.txt
        #

        -rw-r--r-- 1 root root 3584 Jul 23 10:55 file1.txt
        -rwxr-xr-x 1 root root 10 Jul 23 11:16 script.sh

        can you help or advice with 'awk' command to print out the needed values and assign to variable.

        thnx,

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          You can look at what awk returns and how a shell script handles that. You can check out the awk manual page or just take a look at what is being printed out, and how to print the specific fields inside awk.

          IMHO, I think you need a loop in a shell script to get down to the line you want (probably a for loop, but you could you a while if you wanted to), and then if you're on the right line, use awk to print out the values you want. I don't think you need variables, but a shell scripting tutorial would be able to tell you how to do that if you wanted to.
          Last edited by sicarie; Jul 23 '14, 03:41 PM. Reason: Links, updating methodology

          Comment

          • Luuk
            Recognized Expert Top Contributor
            • Mar 2012
            • 1043

            #6
            Code:
            #!/bin/bash
            awk '/text 7 in to use/{ print substr($0,25,5)}/' file1.txt
            you should change the 25, and probable the 5 too

            read here more about string functions in awk

            Comment

            • Luuk
              Recognized Expert Top Contributor
              • Mar 2012
              • 1043

              #7
              But, more advanced, this is also possible

              Code:
              awk -F '.' '/text 7 in to use/{ print substr($NF,6,5) }' file1.txt
              or even:
              Code:
              awk -F '/' '/text 7 in to use/{ print substr($NF,1,5) }' file1.txt

              Comment

              Working...