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. JavaScript中字符操作之大小写转换

    1.toUpperCase()   方法用于把字符串转换为大写 var str = prompt("请输入需转换大写的字符串:"); str = str.toUpperCase() ...

  2. Quick Search FAQ

    Q: Why does it jump to an incorrect page? A: Some categories, such as twitter, may need to log in in ...

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

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

  4. 请教下 f = f.replace('\n', '\r')这条没起作用

    !/usr/bin/env python -- coding: utf-8 -- import json import string import sys reload(sys) sys.setdef ...

  5. spark 性能优化

    1.内存 spark.storage.memoryFraction:很明显,是指spark缓存的大小,默认比例0.6 spark.shuffle.memoryFraction:管理executor中R ...

  6. 【Bitset】重识

    ---------------------------------------------------------------------------- 一题题目: 一题题解: 这个题目哪来入门再好不 ...

  7. 解决VS2015中出现类似于error C4996: 'scanf': This function or variable may be unsafe的安全检查错误

    用习惯了VS老版本的人当刚使用VS2013的时候可能总遇到类似于这样的错误: error C4996: 'scanf': This function or variable may be unsafe ...

  8. 解决maven项目Cannot change version of project facet Dynamic web module to 3.0/3.1

    解决maven项目Cannot change version of project facet Dynamic web module to 3.0 1.打开项目所在目录下的.settings文件夹 打 ...

  9. 搞懂分布式技术4:ZAB协议概述与选主流程详解

    搞懂分布式技术4:ZAB协议概述与选主流程详解 ZAB协议 ZAB(Zookeeper Atomic Broadcast)协议是专门为zookeeper实现分布式协调功能而设计.zookeeper主要 ...

  10. Python 字典的一键多值,即一个键对应多个值

    转自:http://blog.csdn.net/houyj1986/article/details/22624981 #encoding=utf-8 print '中国' #字典的一键多值 print ...