bash script looping two functions

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












I would like to order the 5th column by highest value and select the 4th if it matches the 5th column.




cat TABLE_LIST



C1 C2 C3 C4 C5 C6
3 No ENCRYPTION /opt/oracle/oradata/ORCLCDB/encryption.dbf 8 0
2 No RETENTION /opt/oracle/oradata/ORCLCDB/retention.dbf 5 0
4 No ORACLE /opt/oracle/oradata/ORCLCDB/oracle.dbf 2 0
1 No USERS /opt/oracle/oradata/ORCLCDB/users01.dbf 3 0`


It should look like this.



for file in $C5; do
select $C4 from from dual;
done





select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;`


What I have tried so far is working but it read the output according to the value of C1, but I want to read according to C5, highest to lowest.



for (( x=1; x <= $FILE_COUNT; x++)) ; do
FILE_NAME=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x print $4 $3 == x print $1'
FILE_SIZE=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x print $5 $3 == x print $1'
done


Thanks!










share|improve this question























  • It's unclear what the output should be for the given input.
    – Kusalananda
    Nov 26 at 21:31










  • I just want to grab the values of column C4 order by C5 (highest to lowest).
    – Kwa Arboncana
    Nov 26 at 21:44






  • 1




    Your sample output select /opt/oracle... lines aren't sorted by C5 descending...
    – Jeff Schaller
    Nov 26 at 22:25














up vote
-1
down vote

favorite












I would like to order the 5th column by highest value and select the 4th if it matches the 5th column.




cat TABLE_LIST



C1 C2 C3 C4 C5 C6
3 No ENCRYPTION /opt/oracle/oradata/ORCLCDB/encryption.dbf 8 0
2 No RETENTION /opt/oracle/oradata/ORCLCDB/retention.dbf 5 0
4 No ORACLE /opt/oracle/oradata/ORCLCDB/oracle.dbf 2 0
1 No USERS /opt/oracle/oradata/ORCLCDB/users01.dbf 3 0`


It should look like this.



for file in $C5; do
select $C4 from from dual;
done





select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;`


What I have tried so far is working but it read the output according to the value of C1, but I want to read according to C5, highest to lowest.



for (( x=1; x <= $FILE_COUNT; x++)) ; do
FILE_NAME=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x print $4 $3 == x print $1'
FILE_SIZE=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x print $5 $3 == x print $1'
done


Thanks!










share|improve this question























  • It's unclear what the output should be for the given input.
    – Kusalananda
    Nov 26 at 21:31










  • I just want to grab the values of column C4 order by C5 (highest to lowest).
    – Kwa Arboncana
    Nov 26 at 21:44






  • 1




    Your sample output select /opt/oracle... lines aren't sorted by C5 descending...
    – Jeff Schaller
    Nov 26 at 22:25












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I would like to order the 5th column by highest value and select the 4th if it matches the 5th column.




cat TABLE_LIST



C1 C2 C3 C4 C5 C6
3 No ENCRYPTION /opt/oracle/oradata/ORCLCDB/encryption.dbf 8 0
2 No RETENTION /opt/oracle/oradata/ORCLCDB/retention.dbf 5 0
4 No ORACLE /opt/oracle/oradata/ORCLCDB/oracle.dbf 2 0
1 No USERS /opt/oracle/oradata/ORCLCDB/users01.dbf 3 0`


It should look like this.



for file in $C5; do
select $C4 from from dual;
done





select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;`


What I have tried so far is working but it read the output according to the value of C1, but I want to read according to C5, highest to lowest.



for (( x=1; x <= $FILE_COUNT; x++)) ; do
FILE_NAME=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x print $4 $3 == x print $1'
FILE_SIZE=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x print $5 $3 == x print $1'
done


Thanks!










share|improve this question















I would like to order the 5th column by highest value and select the 4th if it matches the 5th column.




cat TABLE_LIST



C1 C2 C3 C4 C5 C6
3 No ENCRYPTION /opt/oracle/oradata/ORCLCDB/encryption.dbf 8 0
2 No RETENTION /opt/oracle/oradata/ORCLCDB/retention.dbf 5 0
4 No ORACLE /opt/oracle/oradata/ORCLCDB/oracle.dbf 2 0
1 No USERS /opt/oracle/oradata/ORCLCDB/users01.dbf 3 0`


It should look like this.



for file in $C5; do
select $C4 from from dual;
done





select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;`


What I have tried so far is working but it read the output according to the value of C1, but I want to read according to C5, highest to lowest.



for (( x=1; x <= $FILE_COUNT; x++)) ; do
FILE_NAME=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x print $4 $3 == x print $1'
FILE_SIZE=cat $TABLE_LIST|sort -rk5 |awk -vx="$x" '$1 == x print $5 $3 == x print $1'
done


Thanks!







awk sed for






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 at 21:45









Kulfy

1032




1032










asked Nov 26 at 21:26









Kwa Arboncana

33




33











  • It's unclear what the output should be for the given input.
    – Kusalananda
    Nov 26 at 21:31










  • I just want to grab the values of column C4 order by C5 (highest to lowest).
    – Kwa Arboncana
    Nov 26 at 21:44






  • 1




    Your sample output select /opt/oracle... lines aren't sorted by C5 descending...
    – Jeff Schaller
    Nov 26 at 22:25
















  • It's unclear what the output should be for the given input.
    – Kusalananda
    Nov 26 at 21:31










  • I just want to grab the values of column C4 order by C5 (highest to lowest).
    – Kwa Arboncana
    Nov 26 at 21:44






  • 1




    Your sample output select /opt/oracle... lines aren't sorted by C5 descending...
    – Jeff Schaller
    Nov 26 at 22:25















It's unclear what the output should be for the given input.
– Kusalananda
Nov 26 at 21:31




It's unclear what the output should be for the given input.
– Kusalananda
Nov 26 at 21:31












I just want to grab the values of column C4 order by C5 (highest to lowest).
– Kwa Arboncana
Nov 26 at 21:44




I just want to grab the values of column C4 order by C5 (highest to lowest).
– Kwa Arboncana
Nov 26 at 21:44




1




1




Your sample output select /opt/oracle... lines aren't sorted by C5 descending...
– Jeff Schaller
Nov 26 at 22:25




Your sample output select /opt/oracle... lines aren't sorted by C5 descending...
– Jeff Schaller
Nov 26 at 22:25










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










According to comments you want to list the 4th column in the order dictated by the 5th column (in reverse numerical order).



$ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' print $4 '
/opt/oracle/oradata/ORCLCDB/encryption.dbf
/opt/oracle/oradata/ORCLCDB/retention.dbf
/opt/oracle/oradata/ORCLCDB/users01.dbf
/opt/oracle/oradata/ORCLCDB/oracle.dbf


The sed command strips the header from the file and the sort sorts the remaining lines on the 5th column in decreasing numerical order. The final awk extracts the 4th column.



This would work as long as none of the columns contained a whitespace character within the column itself.




Would you want to insert those select and from dual; strings, then you could modify the awk part of the pipeline:



$ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' printf("select %s from dual;n", $4) '
select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;





share|improve this answer






















  • This is perfect!! Thanks a lot @kusalananda it worked!!
    – Kwa Arboncana
    Nov 26 at 22:56

















up vote
0
down vote













Using GNU Awk > 4.0



gawk '
FNR>1 a[$5] = $4
END
PROCINFO["sorted_in"] = "@ind_num_desc";
for (i in a) print "select " a[i] " from dual;"

' TABLE_LIST
select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;





share|improve this answer




















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484304%2fbash-script-looping-two-functions%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    According to comments you want to list the 4th column in the order dictated by the 5th column (in reverse numerical order).



    $ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' print $4 '
    /opt/oracle/oradata/ORCLCDB/encryption.dbf
    /opt/oracle/oradata/ORCLCDB/retention.dbf
    /opt/oracle/oradata/ORCLCDB/users01.dbf
    /opt/oracle/oradata/ORCLCDB/oracle.dbf


    The sed command strips the header from the file and the sort sorts the remaining lines on the 5th column in decreasing numerical order. The final awk extracts the 4th column.



    This would work as long as none of the columns contained a whitespace character within the column itself.




    Would you want to insert those select and from dual; strings, then you could modify the awk part of the pipeline:



    $ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' printf("select %s from dual;n", $4) '
    select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;





    share|improve this answer






















    • This is perfect!! Thanks a lot @kusalananda it worked!!
      – Kwa Arboncana
      Nov 26 at 22:56














    up vote
    1
    down vote



    accepted










    According to comments you want to list the 4th column in the order dictated by the 5th column (in reverse numerical order).



    $ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' print $4 '
    /opt/oracle/oradata/ORCLCDB/encryption.dbf
    /opt/oracle/oradata/ORCLCDB/retention.dbf
    /opt/oracle/oradata/ORCLCDB/users01.dbf
    /opt/oracle/oradata/ORCLCDB/oracle.dbf


    The sed command strips the header from the file and the sort sorts the remaining lines on the 5th column in decreasing numerical order. The final awk extracts the 4th column.



    This would work as long as none of the columns contained a whitespace character within the column itself.




    Would you want to insert those select and from dual; strings, then you could modify the awk part of the pipeline:



    $ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' printf("select %s from dual;n", $4) '
    select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;





    share|improve this answer






















    • This is perfect!! Thanks a lot @kusalananda it worked!!
      – Kwa Arboncana
      Nov 26 at 22:56












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    According to comments you want to list the 4th column in the order dictated by the 5th column (in reverse numerical order).



    $ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' print $4 '
    /opt/oracle/oradata/ORCLCDB/encryption.dbf
    /opt/oracle/oradata/ORCLCDB/retention.dbf
    /opt/oracle/oradata/ORCLCDB/users01.dbf
    /opt/oracle/oradata/ORCLCDB/oracle.dbf


    The sed command strips the header from the file and the sort sorts the remaining lines on the 5th column in decreasing numerical order. The final awk extracts the 4th column.



    This would work as long as none of the columns contained a whitespace character within the column itself.




    Would you want to insert those select and from dual; strings, then you could modify the awk part of the pipeline:



    $ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' printf("select %s from dual;n", $4) '
    select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;





    share|improve this answer














    According to comments you want to list the 4th column in the order dictated by the 5th column (in reverse numerical order).



    $ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' print $4 '
    /opt/oracle/oradata/ORCLCDB/encryption.dbf
    /opt/oracle/oradata/ORCLCDB/retention.dbf
    /opt/oracle/oradata/ORCLCDB/users01.dbf
    /opt/oracle/oradata/ORCLCDB/oracle.dbf


    The sed command strips the header from the file and the sort sorts the remaining lines on the 5th column in decreasing numerical order. The final awk extracts the 4th column.



    This would work as long as none of the columns contained a whitespace character within the column itself.




    Would you want to insert those select and from dual; strings, then you could modify the awk part of the pipeline:



    $ sed '1d' <TABLE_LIST | sort -k5,5nr | awk ' printf("select %s from dual;n", $4) '
    select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 26 at 22:03

























    answered Nov 26 at 21:51









    Kusalananda

    118k16223364




    118k16223364











    • This is perfect!! Thanks a lot @kusalananda it worked!!
      – Kwa Arboncana
      Nov 26 at 22:56
















    • This is perfect!! Thanks a lot @kusalananda it worked!!
      – Kwa Arboncana
      Nov 26 at 22:56















    This is perfect!! Thanks a lot @kusalananda it worked!!
    – Kwa Arboncana
    Nov 26 at 22:56




    This is perfect!! Thanks a lot @kusalananda it worked!!
    – Kwa Arboncana
    Nov 26 at 22:56












    up vote
    0
    down vote













    Using GNU Awk > 4.0



    gawk '
    FNR>1 a[$5] = $4
    END
    PROCINFO["sorted_in"] = "@ind_num_desc";
    for (i in a) print "select " a[i] " from dual;"

    ' TABLE_LIST
    select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
    select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;





    share|improve this answer
























      up vote
      0
      down vote













      Using GNU Awk > 4.0



      gawk '
      FNR>1 a[$5] = $4
      END
      PROCINFO["sorted_in"] = "@ind_num_desc";
      for (i in a) print "select " a[i] " from dual;"

      ' TABLE_LIST
      select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
      select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
      select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
      select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Using GNU Awk > 4.0



        gawk '
        FNR>1 a[$5] = $4
        END
        PROCINFO["sorted_in"] = "@ind_num_desc";
        for (i in a) print "select " a[i] " from dual;"

        ' TABLE_LIST
        select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
        select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
        select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
        select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;





        share|improve this answer












        Using GNU Awk > 4.0



        gawk '
        FNR>1 a[$5] = $4
        END
        PROCINFO["sorted_in"] = "@ind_num_desc";
        for (i in a) print "select " a[i] " from dual;"

        ' TABLE_LIST
        select /opt/oracle/oradata/ORCLCDB/encryption.dbf from dual;
        select /opt/oracle/oradata/ORCLCDB/retention.dbf from dual;
        select /opt/oracle/oradata/ORCLCDB/users01.dbf from dual;
        select /opt/oracle/oradata/ORCLCDB/oracle.dbf from dual;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 at 0:46









        steeldriver

        33.8k34983




        33.8k34983



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484304%2fbash-script-looping-two-functions%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay