我有一个~23000行的SQL转储包含几个数据库的数据。 我须要提取此文件的某个部分(即单个数据库的数据)并将其放在一个新文件中。 我知道我想要的数据的起始行和结束行号。 数据库
有没有人知道一个Unix命令(或一系列命令)从第16224和16482行之间的文件中提取全部行,而后将它们重定向到一个新文件? bash
我会用: less
awk 'FNR >= 16224 && FNR <= 16482' my_file > extracted.txt
FNR包含从文件中读取的行的记录(行)编号。 ui
我编写了一个名为splitter的Haskell程序,它正是这样作的: 阅读个人发布博客文章 。 this
您能够按以下方式使用该程序: spa
$ cat somefile | splitter 16224-16482
这就是它的所有内容。 您将须要Haskell来安装它。 只是: 命令行
$ cabal install splitter
你完成了。 我但愿你发现这个程序颇有用。 code
awk
还有另外一种方法: three
awk 'NR==16224, NR==16482' file
若是文件很大,最好在读取最后一行后exit
。 这样,它就不会没必要要地读取如下行: terminal
awk 'NR==16224, NR==16482-1; NR==16482 {print; exit}' file
即便咱们能够在命令行检查:
cat filename|sed 'n1,n2!d' > abc.txt
例如:
cat foo.pl|sed '100,200!d' > abc.txt
我写了一个小的bash脚本,您能够从命令行运行,只要您更新PATH以包含其目录(或者您能够将它放在已包含在PATH中的目录中)。
用法:$ pinch filename起始行结束行
#!/bin/bash # Display line number ranges of a file to the terminal. # Usage: $ pinch filename start-line end-line # By Evan J. Coon FILENAME=$1 START=$2 END=$3 ERROR="[PINCH ERROR]" # Check that the number of arguments is 3 if [ $# -lt 3 ]; then echo "$ERROR Need three arguments: Filename Start-line End-line" exit 1 fi # Check that the file exists. if [ ! -f "$FILENAME" ]; then echo -e "$ERROR File does not exist. \n\t$FILENAME" exit 1 fi # Check that start-line is not greater than end-line if [ "$START" -gt "$END" ]; then echo -e "$ERROR Start line is greater than End line." exit 1 fi # Check that start-line is positive. if [ "$START" -lt 0 ]; then echo -e "$ERROR Start line is less than 0." exit 1 fi # Check that end-line is positive. if [ "$END" -lt 0 ]; then echo -e "$ERROR End line is less than 0." exit 1 fi NUMOFLINES=$(wc -l < "$FILENAME") # Check that end-line is not greater than the number of lines in the file. if [ "$END" -gt "$NUMOFLINES" ]; then echo -e "$ERROR End line is greater than number of lines in file." exit 1 fi # The distance from the end of the file to end-line ENDDIFF=$(( NUMOFLINES - END )) # For larger files, this will run more quickly. If the distance from the # end of the file to the end-line is less than the distance from the # start of the file to the start-line, then start pinching from the # bottom as opposed to the top. if [ "$START" -lt "$ENDDIFF" ]; then < "$FILENAME" head -n $END | tail -n +$START else < "$FILENAME" tail -n +$START | head -n $(( END-START+1 )) fi # Success exit 0