1.从原文件中随机选出若干行

可以直接用shuf命令就可以完成:

$ shuf -n  source.txt > target.txt

shuf命令的说明:

$ shuf --help
Usage: shuf [OPTION]... [FILE]
or: shuf -e [OPTION]... [ARG]...
or: shuf -i LO-HI [OPTION]...
Write a random permutation of the input lines to standard output. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too.
-e, --echo treat each ARG as an input line
-i, --input-range=LO-HI treat each number LO through HI as an input line
-n, --head-count=COUNT output at most COUNT lines
-o, --output=FILE write result to FILE instead of standard output
--random-source=FILE get random bytes from FILE
-r, --repeat output lines can be repeated
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit

2.把文件随机切分成若干部分

这里我的做法是先把文件全部打乱,再进行顺序切分

(1)全部打乱

$ shuf source.txt > source_shuffle.txt

(2)顺序切分

切分的方法有很多种:用split、head/tail、awk、sed都可以,根据实际需要选用即可

(可参考:[Linux] 输出文件的指定行Linux 大文件的分割与合并

例如,这里把打乱后的文件根据前100行与剩余的部分作为最终想要的随机切分结果:

$ head -n100 source_shuffle.txt > target1.txt
$ tail -n+ source_shuffle.txt > target2.txt # 或者$ awk 'NR>=101' source_shuffle.txt > target2.txt 

如果有其它更高效便捷的方法也欢迎指教~

[Linux] 随机切分文件内容的更多相关文章

  1. linux系统清空文件内容

    本文转载至:http://www.jbxue.com/LINUXjishu/14410.html 本文介绍下,在linux系统中,清空文件内容的方法,使用cat命令.echo命令,将文件内容截断为0字 ...

  2. [转]linux查看日志文件内容命令

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  3. linux查看日志文件内容命令tail、cat、tac、head、echo

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  4. 【Linux】linux查看日志文件内容命令tail、cat、tac、head、echo

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  5. linux less对文件内容进行搜索

    [ 可以先用 less 文件名 来打开文件, 然后可以按回车,打开底部命令输入行(即出现一个冒号的位置), 然后可以使用  键盘上的 home 键跳到文件开始,end键跳到最后,PgUp向前翻页,Pg ...

  6. linux下直接复制文件内容到剪切板

    title: linux下直接复制文件内容到剪切板 date: 2017-11-23 17:00:06 tags: categories: Linux 首先安装xsel. xsel --input - ...

  7. linux查看日志文件内容命令tail、cat、tac、head、echo、vi

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  8. linux查看日志文件内容命令tail、cat、tac、head、echo详解

    linux查看日志文件内容命令tail.cat.tac.head.echo tail -f test.log你会看到屏幕不断有内容被打印出来. 这时候中断第一个进程Ctrl-C, ---------- ...

  9. 在Linux下查找文件内容包含某个特定字符串的文件

    如何在Linux下查找文件内容包含某个特定字符串的文件? 我的目录下面有test1和test2两个文件夹,里面都含有很多文件,其中test2里面还包含一个test文件夹 我想请问的是,如何通过查找关键 ...

随机推荐

  1. SQL系列 - SQL语句优化个人总结

    关于SQL语句优化方法 有些是通用的(如避免Select *): 有些不同的数据库管理系统有所区别(如Where子句顺序): 然后必须根据实际环境进行调优,因为即使是相同的数据库和表,在数据量或其他环 ...

  2. 推荐个非常简单好用的AOP -- MrAdvice

    https://github.com/ArxOne/MrAdvice 太简单了,写好自己的处理类, 作为Attribute加到要拦截的方法或者类上就可以了. Here is the minimal s ...

  3. Idea 切换git账号

    重置一下账号设置,再次执行拉取或推送会提示重新输入账号密码 进入项目根目录执行:git config --system --unset credential.helper

  4. 01_zookeeper简介(刷新)

    1. 分布式系统及其问题 zookeeper是帮助我们构建分布式系统的一个软件(协调员的角色)首先,我们要明白分布式系统以及它的问题,之后才能理解为什么有zookeeper 1.1 分布式系统 分布式 ...

  5. Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树

    Unique Binary Search Trees:求生成二叉排序树的个数. Given n, how many structurally unique BST's (binary search t ...

  6. ZJOI2017游记

    $Day$ $-1$ 听说可以去$ZJOI2017$打酱油,终于可以出去走走辣$QAQ$... 上次出去打比赛似乎是$PKUSC$?? 好吧,至少可以一览国家预备队爷们的风采... 准备把膝盖留在浙江 ...

  7. [ES6]import 与export的用法 ,export 与export default 的 区别 以及用法

    一.import 与export export(导出):用于对外输出本模块(一个文件可以理解为一个模块)变量的接口: import(导入):用于在一个模块中加载另一个含有export接口的模块. 1. ...

  8. python的os.urandom 的用途

    Python中os.urandom(n)的作用 随机产生n个字节(0-255)的字符串,可以作为随机加密key使用~ >>> index = os.urandom(2) >&g ...

  9. css去掉input记住密码的黄色

    input:-webkit-autofill,input:-webkit-autofill:hover,input:-webkit-autofill:focus,input:-webkit-autof ...

  10. 020——VUE中变异方法push的留言版实例讲解

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...