Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    and buddy one more thing i tried with giving hard coding the value in loop but its not prinitng for 3 times

    awk -F \| -v repl="$var" 'BEGIN {OFS ="|"} {
    $2=repl;
    for (i=0;i<333;i++){
    $3=sprintf("%d",rand()*100+1);
    print;


    wat i need is for eg: in file there 3 records
    i want to multiply the number of 3 *n records and write in output file.
    so no my output file will have 6 records if i give n = 2 as input
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,484
    Rep Power
    1752
    Ok, so you wish each row in the input file to be repeated a number of times determined by your input?
    Leaving any and all other considerations aside:

    Code:
    awk -F \| -v repeats=$var
      'BEGIN {OFS ="|"} 
      {
        for (i=0;i<repeats;i++){
          $3=sprintf("%d",rand()*100+1);
          print;
        }
      }' input_file
    Where $var is the number you have entered
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    23
    Rep Power
    0
    yeah buddy i tried with your above code tat would work printing n number of times based on the input given
    and morever wat i need as well is replace and generate random number as well as per my previous requirements.

    requirement:
    replace a string whatevr the input given by the user
    generate random numbers between 1-100
    print for N number of times

    hence i would execute my script like this
    sh script.sh replace_string input_file_name n(number of time to print)

    the above is wat i am trying now problem facing in printing N number of times if i hard code n then its working.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,484
    Rep Power
    1752
    Ok, in your script $1 is the replacement string and $3 is the repeat count. Those are VERY different from the $1 and $3 variables within awk (which relate to the fields within the current row).

    Your script would/should/could look like:

    Code:
    replacement="$1"
    input_file="$2"
    repeat=$3
    
    awk -F \| -v repeats=$repeat -v repl="$replacement"
      'BEGIN {OFS ="|"} 
      {
        $2=repl;
        for (i=0;i<repeats;i++){
          $3=sprintf("%d",rand()*100+1);
          print;
        }
      }' $input_file
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo