Shweta
Shweta

Reputation: 1161

randomly choose few lines from a file and then choose same line nos from second file

I have 2 files, say A.txt and B.txt, each having 100 lines. I want to choose 10 records randomly from file A (which can easily be done with linux shuf command). But now I want same line nos data from file B also. Is there any easy way to do it via linux command line.

For Example: Following are the records from 2 files,
A1........A100
B1........B100

If 10 random records generated from A are A1, A3, A9, etc, then i want B1, B3, B9, etc from file B.

Upvotes: 0

Views: 71

Answers (2)

Marqin
Marqin

Reputation: 1094

This bash script will do this, to two files provided as args:

for i in `seq 1 10`; do
  NR=$(($RANDOM % 100 + 1))
  echo -n "File $1, line $NR: "
  sed "$NR"'q;d' "$1"
  echo -n "File $2, line $NR: "
  sed "$NR"'q;d' "$2"
done

eg. usage:

bash ./get10.sh long_file.txt longfile.txt

Upvotes: 2

Tom Fenech
Tom Fenech

Reputation: 74605

One option would be to paste the two files together:

paste file_a file_b | shuf -n 10

To separate the two files, you could use awk:

paste file_a file_b | shuf -n 10 | awk '{ print $1 > "a_sample"; print $2 > "b_sample" }'

By default, paste joins the two files together with a tab character in between, which works with awk's default field separator. If the actual data in file_a and file_b contains spaces (but no tabs), you can set the input field separator to only tabs using awk -F'\t'.

Upvotes: 2

Related Questions