Home > Software design >  Move X amount of lines from one text file to another using bash
Move X amount of lines from one text file to another using bash

Time:09-30

I am trying to combine two TXT files somewhat randomly and to do this I am trying to use head and sed to move X amount of lines to a new file and delete the lines from the old file. The problem is, because it is a random value, I can't tell sed just how many lines to delete. Here is what I tried to use that doesn't work as desired:

head -$(shuf -i 3-6 -n1) firstfile.txt > combine.txt && sed -i '1, $(shuf -i 2-5 -n1)d' firstfile.txt

The problem with the above code is the second use of shuf does not match what the first one is. For example, the first shuf could be 5 and the second shuf could be 3. But I want the second shuf to always be first shuf -1 (so if 1st shuf is 5, second should be 4)

Any solution where I can get the second shuf to match the first or an easier way to do this?

CodePudding user response:

You can save the result of shuf in a variable and reuse it.

shuf_value=$(shuf -i 3-6 -n1)
head -"${shuf_value}" firstfile.txt > combine.txt &&
sed -i "1, $(( shuf_value-1 ))d" firstfile.txt

CodePudding user response:

You can do it using a single GNU sed command without resorting to head or a temporary variable:

sed -ni "1,$(shuf -i 3-6 -n1)!{p;d;}; w combine.txt" firstfile.txt
  • Related