Retrieve Field Rows from a Pipe Delimited File









up vote
0
down vote

favorite












I need to get first 9 words from a pipe delimited file and then next 9 words. Any help is appreciated.



cat a.txt
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|

cat new.ksh
#! /bin/ksh
a=`awk -F "|" ' print NF-1 ' a.txt`
echo $a

Expected Output:
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z









share|improve this question























  • What was the reasoning behind using print NF-1? How does it correlate with your expected output?
    – hek2mgl
    Nov 9 at 17:40















up vote
0
down vote

favorite












I need to get first 9 words from a pipe delimited file and then next 9 words. Any help is appreciated.



cat a.txt
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|

cat new.ksh
#! /bin/ksh
a=`awk -F "|" ' print NF-1 ' a.txt`
echo $a

Expected Output:
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z









share|improve this question























  • What was the reasoning behind using print NF-1? How does it correlate with your expected output?
    – hek2mgl
    Nov 9 at 17:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to get first 9 words from a pipe delimited file and then next 9 words. Any help is appreciated.



cat a.txt
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|

cat new.ksh
#! /bin/ksh
a=`awk -F "|" ' print NF-1 ' a.txt`
echo $a

Expected Output:
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z









share|improve this question















I need to get first 9 words from a pipe delimited file and then next 9 words. Any help is appreciated.



cat a.txt
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|

cat new.ksh
#! /bin/ksh
a=`awk -F "|" ' print NF-1 ' a.txt`
echo $a

Expected Output:
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z






bash shell unix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 17:10

























asked Nov 9 at 16:48









Josh

417




417











  • What was the reasoning behind using print NF-1? How does it correlate with your expected output?
    – hek2mgl
    Nov 9 at 17:40

















  • What was the reasoning behind using print NF-1? How does it correlate with your expected output?
    – hek2mgl
    Nov 9 at 17:40
















What was the reasoning behind using print NF-1? How does it correlate with your expected output?
– hek2mgl
Nov 9 at 17:40





What was the reasoning behind using print NF-1? How does it correlate with your expected output?
– hek2mgl
Nov 9 at 17:40













3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










sed -r 's/([^|]*|)9/&n/g' a.txt


Explanation:
[^|] Not a |.
[^|]*| One field (without |), followed by a |.
([^|]*|)9 9 fields, each followed by a |.
&n Replace by the matched string followed by a newline.






share|improve this answer




















  • thanks for your response. I will give it a shot
    – Josh
    Nov 10 at 0:50










  • Very nice elegant solution. Minor note, it gives a trailing bar (|) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
    – user1683793
    Nov 10 at 1:30

















up vote
1
down vote













Try this:



mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
mayankp@mayank:~/$ echo >> output.txt
mayankp@mayank:~/$ cat output.txt
a|b|c|d|e|f|g|h|i|
j|k|l|m|n|o|p|q|r|
s|t|u|v|w|x|y|z||





share|improve this answer




















  • @Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
    – Josh
    Nov 9 at 17:57

















up vote
1
down vote













I pure ksh version:



IFS='|'
typeset -a str=( $( < a.txt ) )
len=$#str[@]
bar='|'
for ((ii=0; ii<len; ii+=9))
do
max=9
(( ii+9 > len )) && max=$(( len-ii ))
for ((jj=0; jj<max; jj++))
do
element=$(( ii+jj ))
(( element == len-1 )) && bar=''
echo -e "$str[$element]$barc"
done
echo
done


Explanation:




  • IFS: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.


  • typeset -a We are creating an array to hold the data.


  • str=( $( < a.txt ) ) We read in the data from the a.txt array as elements of the array.


  • len=$#str[@] We need to know how long the array is so we know how much to print out.

  • Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.

  • To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.


  • (( ii+9 > len )) && max=$(( len-ii )) This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.

  • Inner for loop, this where we step through the elements of the line.


  • element=... We want the ii+jj element so here we calculate the value.


  • (( element == len-1 )) && bar='' You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.


  • echo -e "$str[$element]$barc" Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.


  • echo Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.

This version also works with bash.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53230025%2fretrieve-field-rows-from-a-pipe-delimited-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    sed -r 's/([^|]*|)9/&n/g' a.txt


    Explanation:
    [^|] Not a |.
    [^|]*| One field (without |), followed by a |.
    ([^|]*|)9 9 fields, each followed by a |.
    &n Replace by the matched string followed by a newline.






    share|improve this answer




















    • thanks for your response. I will give it a shot
      – Josh
      Nov 10 at 0:50










    • Very nice elegant solution. Minor note, it gives a trailing bar (|) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
      – user1683793
      Nov 10 at 1:30














    up vote
    2
    down vote



    accepted










    sed -r 's/([^|]*|)9/&n/g' a.txt


    Explanation:
    [^|] Not a |.
    [^|]*| One field (without |), followed by a |.
    ([^|]*|)9 9 fields, each followed by a |.
    &n Replace by the matched string followed by a newline.






    share|improve this answer




















    • thanks for your response. I will give it a shot
      – Josh
      Nov 10 at 0:50










    • Very nice elegant solution. Minor note, it gives a trailing bar (|) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
      – user1683793
      Nov 10 at 1:30












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    sed -r 's/([^|]*|)9/&n/g' a.txt


    Explanation:
    [^|] Not a |.
    [^|]*| One field (without |), followed by a |.
    ([^|]*|)9 9 fields, each followed by a |.
    &n Replace by the matched string followed by a newline.






    share|improve this answer












    sed -r 's/([^|]*|)9/&n/g' a.txt


    Explanation:
    [^|] Not a |.
    [^|]*| One field (without |), followed by a |.
    ([^|]*|)9 9 fields, each followed by a |.
    &n Replace by the matched string followed by a newline.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 9 at 23:09









    Walter A

    10.1k2930




    10.1k2930











    • thanks for your response. I will give it a shot
      – Josh
      Nov 10 at 0:50










    • Very nice elegant solution. Minor note, it gives a trailing bar (|) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
      – user1683793
      Nov 10 at 1:30
















    • thanks for your response. I will give it a shot
      – Josh
      Nov 10 at 0:50










    • Very nice elegant solution. Minor note, it gives a trailing bar (|) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
      – user1683793
      Nov 10 at 1:30















    thanks for your response. I will give it a shot
    – Josh
    Nov 10 at 0:50




    thanks for your response. I will give it a shot
    – Josh
    Nov 10 at 0:50












    Very nice elegant solution. Minor note, it gives a trailing bar (|) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
    – user1683793
    Nov 10 at 1:30




    Very nice elegant solution. Minor note, it gives a trailing bar (|) that was not in the "expected" output but if the trailing bar is left off the input, it doesn't appear in the output.
    – user1683793
    Nov 10 at 1:30












    up vote
    1
    down vote













    Try this:



    mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat output.txt
    a|b|c|d|e|f|g|h|i|
    j|k|l|m|n|o|p|q|r|
    s|t|u|v|w|x|y|z||





    share|improve this answer




















    • @Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
      – Josh
      Nov 9 at 17:57














    up vote
    1
    down vote













    Try this:



    mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat output.txt
    a|b|c|d|e|f|g|h|i|
    j|k|l|m|n|o|p|q|r|
    s|t|u|v|w|x|y|z||





    share|improve this answer




















    • @Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
      – Josh
      Nov 9 at 17:57












    up vote
    1
    down vote










    up vote
    1
    down vote









    Try this:



    mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat output.txt
    a|b|c|d|e|f|g|h|i|
    j|k|l|m|n|o|p|q|r|
    s|t|u|v|w|x|y|z||





    share|improve this answer












    Try this:



    mayankp@mayank:~/$ cat a.txt| tr "|" "n" |head -9 | tr "n" "|" > output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -18|head -9| tr "n" "|" >> output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat a.txt| tr "|" "n" | tail -9 | tr "n" "|" >> output.txt
    mayankp@mayank:~/$ echo >> output.txt
    mayankp@mayank:~/$ cat output.txt
    a|b|c|d|e|f|g|h|i|
    j|k|l|m|n|o|p|q|r|
    s|t|u|v|w|x|y|z||






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 9 at 17:55









    Mayank Porwal

    2,2541619




    2,2541619











    • @Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
      – Josh
      Nov 9 at 17:57
















    • @Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
      – Josh
      Nov 9 at 17:57















    @Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
    – Josh
    Nov 9 at 17:57




    @Thanks Mayank for your response but I am looking something Dynamic. If the new words are appended then it work.
    – Josh
    Nov 9 at 17:57










    up vote
    1
    down vote













    I pure ksh version:



    IFS='|'
    typeset -a str=( $( < a.txt ) )
    len=$#str[@]
    bar='|'
    for ((ii=0; ii<len; ii+=9))
    do
    max=9
    (( ii+9 > len )) && max=$(( len-ii ))
    for ((jj=0; jj<max; jj++))
    do
    element=$(( ii+jj ))
    (( element == len-1 )) && bar=''
    echo -e "$str[$element]$barc"
    done
    echo
    done


    Explanation:




    • IFS: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.


    • typeset -a We are creating an array to hold the data.


    • str=( $( < a.txt ) ) We read in the data from the a.txt array as elements of the array.


    • len=$#str[@] We need to know how long the array is so we know how much to print out.

    • Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.

    • To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.


    • (( ii+9 > len )) && max=$(( len-ii )) This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.

    • Inner for loop, this where we step through the elements of the line.


    • element=... We want the ii+jj element so here we calculate the value.


    • (( element == len-1 )) && bar='' You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.


    • echo -e "$str[$element]$barc" Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.


    • echo Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.

    This version also works with bash.






    share|improve this answer


























      up vote
      1
      down vote













      I pure ksh version:



      IFS='|'
      typeset -a str=( $( < a.txt ) )
      len=$#str[@]
      bar='|'
      for ((ii=0; ii<len; ii+=9))
      do
      max=9
      (( ii+9 > len )) && max=$(( len-ii ))
      for ((jj=0; jj<max; jj++))
      do
      element=$(( ii+jj ))
      (( element == len-1 )) && bar=''
      echo -e "$str[$element]$barc"
      done
      echo
      done


      Explanation:




      • IFS: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.


      • typeset -a We are creating an array to hold the data.


      • str=( $( < a.txt ) ) We read in the data from the a.txt array as elements of the array.


      • len=$#str[@] We need to know how long the array is so we know how much to print out.

      • Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.

      • To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.


      • (( ii+9 > len )) && max=$(( len-ii )) This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.

      • Inner for loop, this where we step through the elements of the line.


      • element=... We want the ii+jj element so here we calculate the value.


      • (( element == len-1 )) && bar='' You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.


      • echo -e "$str[$element]$barc" Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.


      • echo Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.

      This version also works with bash.






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        I pure ksh version:



        IFS='|'
        typeset -a str=( $( < a.txt ) )
        len=$#str[@]
        bar='|'
        for ((ii=0; ii<len; ii+=9))
        do
        max=9
        (( ii+9 > len )) && max=$(( len-ii ))
        for ((jj=0; jj<max; jj++))
        do
        element=$(( ii+jj ))
        (( element == len-1 )) && bar=''
        echo -e "$str[$element]$barc"
        done
        echo
        done


        Explanation:




        • IFS: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.


        • typeset -a We are creating an array to hold the data.


        • str=( $( < a.txt ) ) We read in the data from the a.txt array as elements of the array.


        • len=$#str[@] We need to know how long the array is so we know how much to print out.

        • Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.

        • To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.


        • (( ii+9 > len )) && max=$(( len-ii )) This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.

        • Inner for loop, this where we step through the elements of the line.


        • element=... We want the ii+jj element so here we calculate the value.


        • (( element == len-1 )) && bar='' You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.


        • echo -e "$str[$element]$barc" Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.


        • echo Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.

        This version also works with bash.






        share|improve this answer














        I pure ksh version:



        IFS='|'
        typeset -a str=( $( < a.txt ) )
        len=$#str[@]
        bar='|'
        for ((ii=0; ii<len; ii+=9))
        do
        max=9
        (( ii+9 > len )) && max=$(( len-ii ))
        for ((jj=0; jj<max; jj++))
        do
        element=$(( ii+jj ))
        (( element == len-1 )) && bar=''
        echo -e "$str[$element]$barc"
        done
        echo
        done


        Explanation:




        • IFS: This is the Internal field separator, normally this is space and tab but if it is reassigned, it separates other types of data.


        • typeset -a We are creating an array to hold the data.


        • str=( $( < a.txt ) ) We read in the data from the a.txt array as elements of the array.


        • len=$#str[@] We need to know how long the array is so we know how much to print out.

        • Outer for loop, you wanted to see the values in groups of 9. This steps through the array nine at a time.

        • To avoid running off the end, I created a variable max that will hold how many elements are on the current line. The we guess 9 and then updated it if necessary.


        • (( ii+9 > len )) && max=$(( len-ii )) This is doing an integer comparison of ii+9 against the length. If the condition is true, we update max with the lower value.

        • Inner for loop, this where we step through the elements of the line.


        • element=... We want the ii+jj element so here we calculate the value.


        • (( element == len-1 )) && bar='' You had no bar at the end. To avoid printing the bar, we use a bar variable that we turn to nothing when done.


        • echo -e "$str[$element]$barc" Here we print that element along with the bar variable. The c says do not end the print with a newline. The -e is required to get the c to work.


        • echo Since we did not get linefeeds for each element, we need to put it out now that we have 9 elements.

        This version also works with bash.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 1:50

























        answered Nov 10 at 1:05









        user1683793

        658512




        658512



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230025%2fretrieve-field-rows-from-a-pipe-delimited-file%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo