Quick Unix Command to Display Specific Lines in the Middle of a File

Quick unix command to display specific lines in the middle of a file?

with GNU-grep you could just say

grep --context=10 ...

How to get some specific lines from huge text file in unix?

To print line N, use:

sed 'Nq;d' file

To print multiple lines (assuming they are in ascending order) e.g. 994123, 1002451, 1010123:

sed '994123p;1002451p;1010123q;d' file

The q after the last line number tells sed to quit when it reaches the 1010123th line, instead of wasting time by looping over the remaining lines that we are not interested in. That is why it is efficient on large files.

How can I print specific lines from a file in Unix?

Assuming the line numbers to be printed are sorted.

open my $fh, '<', 'line_numbers' or die $!;
my @ln = <$fh>;
open my $tx, '<', 'text_file' or die $!;
foreach my $ln (@ln) {
my $line;
do {
$line = <$tx>;
} until $. == $ln and defined $line;
print $line if defined $line;
}

Get specific line from unix command output

You can combine head and tail, as follows:

doSomething | head -n 2 | tail -n 1

The head -n 2 shows the first two output lines, the tail -n 1 the last of those two.

For putting this into a variable:

variable=$(doSomething | head -n 2 | tail -n 1)

Quick Unix command to print non-contiguous lines from a text file?

You can ask for specific lines by number like this:

sed -n '1p;5p;7p' my_file

The -n flag means, "don't print lines by default", and then for each line you want, you specify the line number and the p (print) command.

How can I extract a predetermined range of lines from a text file on Unix?


sed -n '16224,16482p;16483q' filename > newfile

From the sed manual:

p -
Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option.

n -
If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If
there is no more input then sed exits without processing any more
commands.

q -
Exit sed without processing any more commands or input.
Note that the current pattern space is printed if auto-print is not disabled with the -n option.

and

Addresses in a sed script can be in any of the following forms:

number
Specifying a line number will match only that line in the input.

An address range can be specified by specifying two addresses
separated by a comma (,). An address range matches lines starting from
where the first address matches, and continues until the second
address matches (inclusively).

Insert line in the middle of file with standard unix tools

Perhaps you are looking for sed's r command?

sed '123r file.txt' main.txt

inserts the contents of file.txt at line 123 of main.txt, printing everything to standard output.

(If your sed has the -i option, you can make it modify main.txt directly; otherwise, it will not modify its input files.)

Using combination of head and tail to display middle line of the file in Unix


head -2 myownfile | tail -1 

should do what you want



Related Topics



Leave a reply



Submit