Copy lines from x to y to another file


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy lines from x to y to another file
# 1  
Copy lines from x to y to another file

OS : RHEL 7.2
Shell : bash

I have a file which has lines like below

I want to copy from 2nd line to the 6th line and copy(redirect) those lines to another file.


Code:
$ cat patterns.txt 
hello world 
hello asia 
hello europe 
hello africa 
hello america 
hello antartica 
hello australia 
some more hellos

So, the expected output will be like below.
Any way I could achieve this ?

Code:
$ cat patterns2.txt 
hello asia 
hello europe 
hello africa 
hello america 
hello antartica 
hello australia

# 2  
Did you consider the head and tail *nix commands?
This User Gave Thanks to RudiC For This Post:
# 3  
Thanks for the tip Rudic

I am trying to print line 2 to 7th line using head command. But, I get the below errors. Can you guide me on the syntax.

Code:
$ head -n 2,7 patterns.txt
head: invalid number of lines: 2,7
$ 
$ 
$ head -n 2 7 patterns.txt
head: cannot open '7' for reading: No such file or directory
==> patterns.txt <==
hello world
hello asia
$ 
$ head -n 2-7 patterns.txt
head: invalid number of lines: 2-7

# 4  
Try

Code:
head -7 file | tail +2
hello asia 
hello europe 
hello africa 
hello america 
hello antartica 
hello australia

This User Gave Thanks to RudiC For This Post:
# 5  
Thanks Rudic

Your solution didn't work for me. Maybe bcoz the head and tail commands I use is GNU.

Code:
$ cat patterns.txt
hello world
hello asia
hello europe
hello africa
hello america
hello antartica
hello australia
some more hellos
$ 
$ head -7 patterns.txt
hello world
hello asia
hello europe
hello africa
hello america
hello antartica
hello australia
$ 
$ 
$ 
$ head -7 patterns.txt | tail +2
tail: cannot open '+2' for reading: No such file or directory
$ 
$ 
$ head -7 patterns.txt | tail 2
tail: cannot open '2' for reading: No such file or directory
$

BTW Is there sed/awk way to do this ?
# 6  
It frequently helps to consult the man pages. man tail:
Quote:
-n, --lines=[+]NUM
output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM
This User Gave Thanks to RudiC For This Post:
# 7  
You can also use awk:

Code:
awk "NR >= $from_line && NR <= $to_line {print}" $filename

This User Gave Thanks to rovf For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #244
Difficulty: Medium
Networking research in the early 1970s by Robert E. Kahn and Vint Cerf let to the formulation of the Transmission Control Program (TCP), and its specification in December 1974 in RFC 699.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy only some lines from very big file?

Dear all, I have stuck with this problem for some days. I have a very big file, this file can not open by vi command. There are 200 loops in this file, in each loop will have one line like this: GWA quasiparticle energy with Z factor (eV) And I need 98 lines next after this line. Is... (6 Replies)
Discussion started by: phamnu
6 Replies

2. Shell Programming and Scripting

How to copy lines that starts with either 3 or 4 into new file?

Hi Guys, I have an awk script that would search the input file for line that starts with a number 3 and copies into a new text file. I want to extend this script to find the lines that either starts with 3 or a or b and copy all those lines into the new file. Here is what I have so far:... (1 Reply)
Discussion started by: Amith821
1 Replies

3. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

4. Shell Programming and Scripting

Copy selective lines from text file

Hello, I have a text file which I need to check for presence of certain tags, and then copy a subsequent portion of text into another file. The tag matching canbe done with Grep but I do not know how to copy selective lines from one file to another. Is it possible do that? I checked up some... (8 Replies)
Discussion started by: ajayram
8 Replies

5. UNIX for Dummies Questions & Answers

copy and paste certain many lines of huge file in linux

Dear All, I am working with windoes OS but remote a linux machine. I wonder the way to copy an paste some part of a huge file in linux machine. the contain of file like as follow: ... dump annealling all custom 10 anneal_*.dat id type x y z q timestep 0.02 run 200000 Memory... (2 Replies)
Discussion started by: ariesto
2 Replies

6. Shell Programming and Scripting

Grep: Copy all lines from log file into new file

Hello everyone. I have a log file that contains multiple domains: www.thisdomain.com agent.thisdomain.com that.thisdomain.com I need to copy all of the lines that contain "www.thisdomain.com" from the log and output them into a new file. I've tried everything with little luck. Please help... (3 Replies)
Discussion started by: aberli
3 Replies

7. Shell Programming and Scripting

Script to capture new lines in a file and copy it to new file

Hi All, I have a file that gives me new line/output every 5 minutes. I need to create a script that capture new line/output besides "IN CRON_STATUS", in this case the new output is "begin ------ cron_status.sh - -----------". I want this script to capture the line starting from "begin ------... (0 Replies)
Discussion started by: fara_aris
0 Replies

8. UNIX for Dummies Questions & Answers

copy lines from opne file to another

Hi all, I'm new to shell scripting. I want to copy the first N lines from a file to another file. Can someone please tell me how this can be done. Thanks Himi (2 Replies)
Discussion started by: HIMANI
2 Replies

9. Shell Programming and Scripting

How to copy multiple lines from a file to another using AWK?

Hi, I have a abc.txt file. In this file there is a SQL query which Iwant to copy and print it on another file.The query (for eg) is written like this: SELECT field1, field2, field3 from table1,table2 where <conditions> END I want to copy this query to another... (3 Replies)
Discussion started by: jisha
3 Replies

10. Shell Programming and Scripting

Copy only the initial 10 lines from a file to another

Hi all, I'm new to shell scripting. I want to copy initial few lines(say first 10 lines) from a file to another file. There is no "head" command in our embedded system. sed & awk is there which I believe will do that, but I dont know how to. This is linux 2.6 (embedded) So please help me.... (5 Replies)
Discussion started by: jockey007
5 Replies

Featured Tech Videos