如何从Unix上的文本文件中提取预定范围的行?

梦想与她 提交于 2020-02-26 03:25:47

我有一个~23000行的SQL转储包含几个数据库的数据。 我需要提取此文件的某个部分(即单个数据库的数据)并将其放在一个新文件中。 我知道我想要的数据的起始行和结束行号。

有没有人知道一个Unix命令(或一系列命令)从第16224和16482行之间的文件中提取所有行,然后将它们重定向到一个新文件?


#1楼

我会用:

awk 'FNR >= 16224 && FNR <= 16482' my_file > extracted.txt

FNR包含从文件中读取的行的记录(行)编号。


#2楼

我编写了一个名为splitter的Haskell程序,它正是这样做的: 阅读我的发布博客文章

您可以按如下方式使用该程序:

$ cat somefile | splitter 16224-16482

这就是它的全部内容。 您将需要Haskell来安装它。 只是:

$ cabal install splitter

你完成了。 我希望你发现这个程序很有用。


#3楼

awk还有另一种方法:

awk 'NR==16224, NR==16482' file

如果文件很大,最好在读取最后一行后exit 。 这样,它就不会不必要地读取以下行:

awk 'NR==16224, NR==16482-1; NR==16482 {print; exit}' file

#4楼

即使我们可以在命令行检查:

cat filename|sed 'n1,n2!d' > abc.txt

例如:

cat foo.pl|sed '100,200!d' > abc.txt

#5楼

我写了一个小的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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!