Copy Specific Line from Less

Going to a specific line number using Less in Unix

With n being the line number:

  • ng: Jump to line number n. Default is the start of the file.
  • nG: Jump to line number n. Default is the end of the file.

So to go to line number 320123, you would type 320123g.

Copy-pasted straight from Wikipedia.

copy less wrapped line results in broken lines

The terminal normally doesn't know you have a wrapped line. Performing a copy with the mouse will result in a rectangular area being selected (a number of lines and columns of characters).That being said, different implementation of terminals might "know" you've got a wrapped line.

how to copy lines 10 to 15 of a file into another file, in unix?

Open a terminal with a shell then

sed -n '10,15p' file1.txt > file2.txt

Simple & easy.

If you want to append to the end instead of wiping file2.txt, use >> for redirection.

sed -n '10,15p' file1.txt >> file2.txt
^^

AWK is also a powerful command line text manipulator:

awk 'NR>=10 && NR<=15' file1.txt > file2.txt

How to copy data from file to another file starting from specific line

Here's a way that works well from cron. Less chance of losing data or corrupting the file:

# preserve first lines of results
head -3 results.txt > results.TMP

# append new data
cat data.txt >> results.TMP

# rename output file atomically in case of system crash
mv results.TMP results.txt

How to copy the first few lines of a giant file, and add a line of text at the end of it using some Linux commands?

The head command can get the first n lines. Variations are:

head -7 file
head -n 7 file
head -7l file

which will get the first 7 lines of the file called "file". The command to use depends on your version of head. Linux will work with the first one.

To append lines to the end of the same file, use:

echo 'first line to add' >> file
echo 'second line to add' >> file
echo 'third line to add' >> file

or:

echo 'first line to add
second line to add
third line to add' >> file

to do it in one hit.

So, tying these two ideas together, if you wanted to get the first 10 lines of the input.txt file to output.txt and append a line with five "=" characters, you could use something like:

( head -10 input.txt ; echo '=====' ) > output.txt

In this case, we do both operations in a sub-shell so as to consolidate the output streams into one, which is then used to create or overwrite the output file.

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).

Get specific line from text file using just shell script

sed:

sed '5!d' file

awk:

awk 'NR==5' file


Related Topics



Leave a reply



Submit