Print a file skipping first X lines in Bash

2019-01-08 02:41发布

I have a very long file which I want to print but skipping the first 1e6 lines for example. I look into the cat man page but I did not see any option to do this. I am looking for a command to do this or a simple bash program.

13条回答
迷人小祖宗
2楼-- · 2019-01-08 03:13
sed -n '1d;p'

this command will delete the first line and print the rest

查看更多
聊天终结者
3楼-- · 2019-01-08 03:14

This shell script works fine for me:

#!/bin/bash
awk -v initial_line=$1 -v end_line=$2 '{
    if (NR >= initial_line && NR <= end_line) 
    print $0
}' $3

Used with this sample file (file.txt):

one
two
three
four
five
six

The command (it will extract from second to fourth line in the file):

edu@debian5:~$./script.sh 2 4 file.txt

Output of this command:

two
three
four

Of course, you can improve it, for example by testing that all argument values are the expected :-)

查看更多
迷人小祖宗
4楼-- · 2019-01-08 03:14
cat < File > | awk '{if(NR > 6) print $0}'
查看更多
一夜七次
5楼-- · 2019-01-08 03:16

if you want to skip first two line
tail -n +3 <filename>

if you want to skip first x line
tail -n +$((x+1)) <filename>

查看更多
成全新的幸福
6楼-- · 2019-01-08 03:18

If you want to see first 10 line you can use sed as below:

sed -n '1,10 p' myFile.txt

or if you want to see lines from 20 to 30 you can use:

sed -n '20,30 p' myFile.txt
查看更多
混吃等死
7楼-- · 2019-01-08 03:18

Use the sed delete command with a range address. For example:

$ sed 1,100d file.txt # Print file.txt omitting lines 1-100.

Alternatively, if you want to only print a known range use the print command with the -n flag:

$ sed -n 201,300p file.txt # Print lines 201-300 from file.txt

This solution should work reliably on all UNIX systems, regardless of the presence of GNU utilities.

查看更多
登录 后发表回答