Moving or copying first rows and last rows into another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving or copying first rows and last rows into another file
# 1  
Old 08-07-2013
Moving or copying first rows and last rows into another file

Hi I would like to move the first 1000 rows of my file into an output file and then move the last 1000 rows into another output file.

Any help would be great

Thanks
# 2  
Old 08-07-2013
see man head and man tail ...
Code:
head -n 1000 infile > outfile1
tail -n 1000 infile > outfile2

This User Gave Thanks to Just Ice For This Post:
# 3  
Old 08-07-2013
also what if I move the middle 1000 (lets say row 45-1045) to a new file

also what if I move the middle 1000 (lets say row 45-1045) to a new file

sorry a bit more complicated then before
# 4  
Old 08-07-2013
Code:
head -1000 > output1
tail -1000 > output2

# 5  
Old 08-07-2013
see man sed ... sample below sets startline at 45 and endline at 1045 and then prints (p) both lines as well as all lines between them ...
Code:
sed -n "45,1045p" infile > outfile3

# 6  
Old 08-07-2013
Quote:
Originally Posted by kylle345
also what if I move the middle 1000 (lets say row 45-1045) to a new file

sorry a bit more complicated then before
Code:
sed -n '45,1045p' infile > output3

# 7  
Old 08-07-2013
For a large file, it's a good idea to add a quit command to those sed scripts so that they don't pointlessly read the rest of the file.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading and copying a selected rows

Dear All, I have a data file input.res like below. (Only six column shown here for example.) Sequence of first column starting from 1 to 148. Input file 1 Q0 9_August_2014_Entertainment2 0 20.14967806339729 BM25b1.0 1 Q0 13_October_2012_Page323 1 20.134224346765738 BM25b1.0 1 Q0... (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

2. Shell Programming and Scripting

Merging rows using two common rows.

Hi.. My requirement is simple but unable to get that.. File 1 : 3 415 A G 4 421 G . 39 421 G A 2 421 G A,C 41 427 A . 4 427 A C 42 436 G . 3 436 G C 43 445 C . 2 445 C T 41 447 A . Output (4 Replies)
Discussion started by: empyrean
4 Replies

3. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

4. Shell Programming and Scripting

Copying down first row in to all the below blank rows in a .csv file

Hi All, I have many of files(.csv) of the format given below. Date,Name,Location 04/02/2012,A,India ,B,China ,C,USA Like this I have 1000's of rows and many columns in all my files. I need a shell script to copy down the Date(in this example column1) to the next 2 rows below(in the... (8 Replies)
Discussion started by: ks_reddy
8 Replies

5. UNIX for Dummies Questions & Answers

moving columns to alternating rows

Hello, I have a space delimited file like this: AAA BBB CCC DDD EEE FFF GGG HHH III And I would like to change it to the following (including the plus signs): AAA BBB + CCC DDD EEE + FFF GGG HHH (2 Replies)
Discussion started by: blakers
2 Replies

6. Shell Programming and Scripting

Split single rows to multiple rows ..

Hi pls help me out to short out this problem rm PAB113_011.out rm: PAB113_011.out: override protection 644 (yes/no)? n If i give y it remove the file. But i added the rm command as a part of ksh file and i tried to remove the file. Its not removing and the the file prompting as... (7 Replies)
Discussion started by: sri_aue
7 Replies

7. Shell Programming and Scripting

Remove 1st two rows and last 2 rows

Hi All, I need to remove 1st 2 line from head and last 2 line from last. I thought it would be possible by using the Head and tail command. But after i am using it is not possible by it. Example:Input file 1 2 3 4 5 Example: Output file 3 But my head and tail command are not... (12 Replies)
Discussion started by: kam786sim
12 Replies

8. Shell Programming and Scripting

delete rows in a file based on the rows of another file

I need to delete rows based on the number of lines in a different file, I have a piece of code with me working but when I merge with my C application, it doesnt work. sed '1,'\"`wc -l < /tmp/fileyyyy`\"'d' /tmp/fileA > /tmp/filexxxx Can anyone give me an alternate solution for the above (2 Replies)
Discussion started by: Muthuraj K
2 Replies

9. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

10. UNIX for Dummies Questions & Answers

Converting rows into multiple-rows

Hi every one; I have a file with 22 rows and 13 columns which includes floating numbers. I want to parse the file so that every five columns in the row would be a new record (row). For example, the first line in the old file should be converted into three lines with first two lines contain 5... (6 Replies)
Discussion started by: PHL
6 Replies
Login or Register to Ask a Question