简体   繁体   English

使用Shell脚本从文件中提取指定的行号

[英]extracting specified line numbers from file using shell script

I have a file with a list of address it looks like this (ADDRESS_FILE) 我有一个带有地址列表的文件,看起来像这样(ADDRESS_FILE)

0xf012134  
0xf932193  
.  
.  
0fx12923a

I have another file with a list of numbers it looks like this (NUMBERS_FILE) 我还有另一个带有数字列表的文件,看起来像这样(NUMBERS_FILE)

20  
40  
.  
.  
12

I want to cut the first 20 lines from ADDRESS_FILE and put that into a new file 我想从ADDRESS_FILE中删除前20行,并将其放入新文件中
then cut the next 40 lines from ADDRESS_FILE so on ... 然后从ADDRESS_FILE剪切下40行,依此类推...

I know that a series of sed commands like the one given below does the job 我知道像下面给出的一系列sed命令可以完成这项工作

sed -n 1,20p ADDRESSS_FILE > temp_file_1
sed -n 20,60p ADDRESSS_FILE > temp_file_2
.  
.
sed -n somenumber,endofilep.  ADDRESS_FILE > temp_file_n

But I want to does this automatically using shell scripting which will change the numbers of lines to cut on each sed execution. 但是我想使用shell脚本自动执行此操作,它将更改每次sed执行时要剪切的行数。

How to do this ??? 这个怎么做 ???

Also on a general note, which are the text processing commands in linux which are very useful in such cases? 同样要注意的是,在这种情况下linux中的哪些文本处理命令非常有用?

Assuming your line numbers are in a file called lines , sorted etc., try: 假设行号在一个名为lines ,sorted等的文件中,请尝试:

#!/bin/sh

j=0
count=1
while read -r i; do
  sed -n $j,$i > filename.$count  # etc... details of sed/redirection elided
  j=$i
  count=$(($count+1))
done < lines

Note. 注意。 The above doesn't assume a consistent number of lines to split on for each iteration. 上面的假设没有为每次迭代分配一致的行数。

Since you've additionally asked for a general utility, try split . 由于您还需要通用工具,因此请尝试split However this splits on a consistent number of lines, and is perhaps of limited use here. 但是,这会分割成一定数量的行,并且在这里可能用途有限。

size=$(wc -l ADDRESSS_FILE)
i=1
n=1
while [ $n -lt $size ]
do
  sed -n $n,$((n+19))p ADDRESSS_FILE > temp_file_$i
  i=$((i+1))
  n=$((n+20))
done

or just 要不就

split -l20 ADDRESSS_FILE temp_file_

(thanks Brian Agnew for the idea). (感谢Brian Agnew的想法)。

Here's an alternative that reads directly from the NUMBERS_FILE : 这是一种直接从NUMBERS_FILE中读取的替代方法:

n=0; i=1
while read; do 
  sed -n ${i},+$(( REPLY - 1 ))p ADDRESS_FILE > temp_file_$(( n++ ))
  (( i += REPLY ))
done < NUMBERS_FILE

An ugly solution which works with a single sed invocation, can probably be made less horrible. 一个可以与单个sed调用一起使用的丑陋解决方案,可能会变得不那么恐怖。

This generates a tiny sed script to split the file 这会生成一个小的sed脚本来拆分文件

#!/bin/bash
sum=0
count=0
sed -n -f <(while read -r n ; do
    echo $((sum+1),$((sum += n)) "w temp_file_$((count++))" ;
done < NUMBERS_FILE) ADDRESS_FILE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM