• + 14 comments

    Mine

    awk 'ORS=NR%2?";":"\n"'
    
    • + 1 comment

      What a beutiful code!

      • + 0 comments

        here is problem solution - https://programs.programmingoneonone.com/2022/02/hackerrank-awk-4-problem-solution.html

    • + 0 comments

      impressive solution man !!

    • + 1 comment

      I didn't quite understand it can you please explain it.

      • + 2 comments

        ORS stands for Output Record Separator. And NR is Number of Record (or just current line number). ORS and NR are just variables and you can do with them what you want. There are also useful FS, OFS, RS, NF, FILENAME, FNR. See http://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/

        • + 1 comment

          wow! good explanation bro!

          • + 0 comments

            not just good, AMAZING!

        • + 4 comments

          what does ? and : mean in this case if you can elaborate more please?

          • + 0 comments

            ? and : is a ternary operator. This kind of syntax a ? b : c appears in many languages and it means "if a then b else c". Read more about that at https://en.wikipedia.org/wiki/%3F%3A

          • + 0 comments

            Ternary operator

          • + 0 comments

            They are ternery operator. CODE1 and CODE2 are equivalent CODE 1 if(condition){ statement1; } else{ statement2; } CODE2 condition?statement1:statement2;

          • + 0 comments

            It's the concatenation

    • + 0 comments

      Clever. Nice demonstration of the use of ORS.

    • [deleted]
      + 0 comments

      Very nice! I used the paste tool to format the output. Can you provide a link as to where is this documented? I would like to read it in more detail.

    • + 0 comments

      excellent code bro!

    • + 0 comments

      Nice solution. Thanks.

    • + 3 comments

      awk 'NR%2 == 1?ORS=";":ORS="\n"' is easier to understand.

      • + 1 comment

        btw your code gives syntax error.

        • + 1 comment

          This code works as follows: awk 'ORS=NR%2?";":"\n"'

          • + 0 comments

            I am asking why the code given by a_programr gave error

      • + 0 comments

        above code gives syntax error. anyone explain please.

    • + 0 comments

      Impressive

    • + 0 comments

      I think this is the only solution which makes the problem interesting! Congratulations, it's great!

    • + 0 comments

      Same code using if else

      awk '{if (NR%2==1) { ORS=";"; print} else {ORS="\n";print;}}'

    • + 0 comments

      Super smart! I learned a lot!

    • + 0 comments

      Does awk print the current line? I tried your code and it worked but I was under the impression that print $0 was required.

    • + 0 comments

      love it ! for people who did't get it :NR Number of record > it usually give us the number of the current record (each line) : 1 2 3 4 our code check of each number of the above, if it's %2 means print ; instead of a new line. else just print new line .