Using and passing arguments to shuf within awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Using and passing arguments to shuf within awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-26-2013
DerSeb DerSeb is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 15 May 2013, 7:26 AM EDT
Posts: 12
Thanks: 8
Thanked 0 Times in 0 Posts
Using and passing arguments to shuf within awk

Hello all,

I would like to output a random number within a range for every line using awk and shuf. I think I'm almost there, but I don't know how to pass arguments to shuf within my awk script:

Input

Code:
1 12190 12227
1 12595 12721
1 13403 13639
1 14362 14829
1 14970 15038

awk:

Code:
awk '{ a = (shuf -i $2"-"$3 -n 1)}{print $1,a}' Input

Desired Output (with second column being a random number between field 2 and 3 of input file):


Code:
1 12211
1 12659
1 13411
1 14705
1 15021

Al I get so far is


Code:
1 012190-122271
1 012595-127211
1 013403-136391
1 014362-148291
1 014970-150381

Any help would be greatly appreciated!

Seb
Sponsored Links
    #2  
Old 03-26-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 22 September 2014, 11:04 AM EDT
Location: India
Posts: 1,828
Thanks: 25
Thanked 524 Times in 503 Posts
Perl, ok?

Code:
perl -lane 'print "$F[0] ", int(rand($F[2]-$F[1])) + $F[1]' file

The Following User Says Thank You to balajesuri For This Useful Post:
DerSeb (03-26-2013)
Sponsored Links
    #3  
Old 03-26-2013
DerSeb DerSeb is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 15 May 2013, 7:26 AM EDT
Posts: 12
Thanks: 8
Thanked 0 Times in 0 Posts
I'm not used to perl, but this works nicely. Thanks!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading a string and passing passing arguments to a while loop goddevil Shell Programming and Scripting 19 01-15-2012 08:48 AM
Passing arguments mnjx UNIX for Dummies Questions & Answers 3 09-18-2010 02:36 PM
Passing arguments to awk kristinu Shell Programming and Scripting 1 06-22-2010 08:16 AM
Passing Arguments-Help Tuxidow Shell Programming and Scripting 5 01-29-2009 08:03 AM
passing arguments jpprial UNIX for Dummies Questions & Answers 4 04-03-2001 11:13 AM



All times are GMT -4. The time now is 08:19 PM.