7 Replies Latest reply on Apr 23, 2019 8:22 PM by happy10319

    extract sid

    happy10319

      Hi,

      I want to extract sid from this string: "server1 database1 user1 profiletarget1"

      Then I run:

      echo "server1 database1 user1 profiletarget1 "|awk -F: '{print "sid="$2}'

      sid=

       

      But sid has no value.

       

      How to do that?

      Thank.

        • 1. Re: extract sid
          Gaz in Oz

          Get rid of -F: as your data is seperated by the space character (the default) not a colon.

          echo "server1 database1 user1 profiletarget1 "|awk '{print "sid="$2}'

          sid=database1

           

          For your reference:

          man awk

          ...

                 -F fs

                 --field-separator fs

                        Use fs for the input field separator (the value of the FS predefined variable).

          • 2. Re: extract sid
            Dude!

            You can do the following:

             

            $ sid=$(echo "server1 database1 user1 profiletarget1" | cut -d' ' -f2)

            $ echo $sid

            database1

            • 3. Re: extract sid
              happy10319

              Thanks to all.

              I have a file: myfile.txt

               

              In which

               

              server1 database1 user1 profiletarget1 
              server1 database1 user2 profiletarget2
              server1 database2 user1 profiletarget3

              I have to extract: sid, user and profile , then I wrote (using your precious advise)

              IFS=$'\n'

              for member in $(cat myfile.txt|grep server1|awk  '{print "sid="$2,"user="$3,"profile="$4}')

              do

              sid=$(echo "$member" | cut -d' ' -f2)

              user=$(echo "$member" | cut -d' ' -f3)

              profile=$(echo "$member" | cut -d' ' -f4)

              echo sid:$sid' user: '$user' profile:  '$profile

              done

              unset IFS

               

              But output is:

               

              Thanks for help.

              • 4. Re: extract sid
                Andris Perkons-Oracle

                I've not completely understood your requirements, but if you just want to extract sid, user and profile for each line, then something like the following should work. Of course there are many other ways how to accomplish this task.

                 

                $ cat myfile.txt
                server1 database1 user1 profiletarget1
                server1 database1 user2 profiletarget2 
                server1 database2 user1 profiletarget3
                

                 

                $ awk '{ print "sid:"$2, "user:"$3, "profile:"$4 }' myfile.txt
                sid:database1 user:user1 profile:profiletarget1
                sid:database1 user:user2 profile:profiletarget2
                sid:database2 user:user1 profile:profiletarget3
                

                 

                OR

                 

                $ while read server sid user profile; do
                > echo sid=$sid user=$user profile=$profile
                >  done < myfile.txt
                sid=database1 user=user1 profile=profiletarget1
                sid=database1 user=user2 profile=profiletarget2
                sid=database2 user=user1 profile=profiletarget3
                

                 

                If you are just looking for lines containing "server1":

                 

                $ cat myfile.txt
                server1 database1 user1 profiletarget1
                server1 database1 user2 profiletarget2 
                server2 database9 user1 profiletarget3
                server2 database8 user1 profiletarget3
                server3 database5 user1 profiletarget3
                server1 database2 user1 profiletarget3
                

                 

                 

                $ awk '{ if ($1=="server1") print "sid:"$2, "user:"$3, "profile:"$4 }' myfile.txt
                sid:database1 user:user1 profile:profiletarget1
                sid:database1 user:user2 profile:profiletarget2
                sid:database2 user:user1 profile:profiletarget3
                

                 

                OR

                 

                $ grep server1 myfile.txt | awk '{ print "sid:"$2, "user:"$3, "profile:"$4 }'
                sid:database1 user:user1 profile:profiletarget1
                sid:database1 user:user2 profile:profiletarget2
                sid:database2 user:user1 profile:profiletarget3
                

                 

                OR

                 

                $ while read server sid user profile; do
                > if [ $server == "server1" ]; then
                >   echo sid=$sid user=$user profile=$profile
                > fi
                > done < myfile.txt
                sid=database1 user=user1 profile=profiletarget1
                sid=database1 user=user2 profile=profiletarget2
                sid=database2 user=user1 profile=profiletarget3
                

                 

                Andris

                • 5. Re: extract sid
                  happy10319

                  Thank you Andris.

                  How can I have :

                      sid=database1

                      user=user1 profile=profiletarget1 

                      user=user2 profile=profiletarget2

                   

                      sid=database2

                      user=user1 profile=profiletarget3

                   

                  Thanks and regards.

                  • 6. Re: extract sid
                    Andris Perkons-Oracle
                    $ cat myfile.txt
                    server1 database1 user1 profiletarget1
                    server2 database9 user1 profiletarget3
                    server2 database8 user1 profiletarget3
                    server3 database5 user1 profiletarget3
                    server1 database2 user1 profiletarget3
                    server1 database1 user2 profiletarget2
                    
                    $ grep server1 myfile.txt | sort -k2 | awk -v osid="" '{ if ($2==osid) { print "user="$3, "profile="$4 } else { print ""; print "sid="$2; print "user="$3, "profile="$4; osid=$2 }}'
                    
                    sid=database1
                    user=user1 profile=profiletarget1
                    user=user2 profile=profiletarget2
                    
                    sid=database2
                    user=user1 profile=profiletarget3
                    
                    • 7. Re: extract sid
                      happy10319

                      Thank Andris.

                      I should write a script like:

                       

                      for member in $(cat myfile.txt | grep server1 | awk '{ print "sid="$2";user="$3";profile="$4 }')

                      do

                       

                      unset sid user profile

                       

                      eval "$member"

                        for member2 in $(cat myfile.txt | grep server1 | awk '{ print "sid1="$2";user1="$3";profile1="$4 }')

                        do

                        unset sid1 user profile

                        eval "$member2"

                        if [ "$sid1" = "$sid" ] ; then

                        echo " we do for $sid'    '  $user1 '  '$profile1"

                        fi

                        done

                       

                      done

                       

                      In which I do process $sid, $user and $profile (instead of echo).

                      The above code unfortunatley iterates more than 3 times (we need only 3 because we have 2 databases. Two lines for database1 +1 line for database2).

                      But the output of my code is:

                      Thanks.