[Request] Copying a file with cp except for the last one or two lines.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Request] Copying a file with cp except for the last one or two lines.
# 1  
Old 05-24-2012
[Request] Copying a file with cp except for the last one or two lines.

Hi folks! I'm new to the unix-side of the world! Smilie and I'm trying to learn as much as possible the good stuff that's in it!
So.. here comes the point.. as you can see in the title, I need to copy a file but truncating it so that last 1-2 lines are not copied... any suggests from the mastercoders? Smilie
# 2  
Old 05-24-2012
i dont think cp has any option to copy like that directly. As a workaround you could do something like below

to copy except last line :
Code:
cat souce_file | sed '$d' > dest_file

to copy except last two lines :
Code:
cat souce_file | sed '$d' |sed '$d'  > dest_file

This User Gave Thanks to ningy For This Post:
# 3  
Old 05-24-2012
Any reason for skipping the last 1-2 lines? Are these "special" in some way from the other lines?
# 4  
Old 05-24-2012
With GNU-head the syntax for copying and cutting the last 2 lines:
Code:
head -n-2 source_file >dest_file

This User Gave Thanks to cero For This Post:
# 5  
Old 05-24-2012
Quote:
Originally Posted by ningy
i dont think cp has any option to copy like that directly. As a workaround you could do something like below

to copy except last line :
Code:
cat souce_file | sed '$d' > dest_file

to copy except last two lines :
Code:
cat souce_file | sed '$d' |sed '$d'  > dest_file

Nice workaround.. what does sed $d do? I can't get it.. (my bad)

Quote:
Originally Posted by elixir_sinari
Any reason for skipping the last 1-2 lines? Are these "special" in some way from the other lines?
Sure Smilie

Quote:
Originally Posted by cero
With GNU-head the syntax for copying and cutting the last 2 lines:
Code:
head -n-2 source_file >dest_file

Ohhh yeah, that's just what I was looking for! Smilie
# 6  
Old 05-24-2012
Quote:
Originally Posted by WideMind
Nice workaround.. what does sed $d do? I can't get it.. (my bad)

$ is the address of the last line of input and d is for deleting the pattern (which in this case is none..so delete the entire line)
This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 05-24-2012
Quote:
Originally Posted by ningy
i dont think cp has any option to copy like that directly. As a workaround you could do something like below

to copy except last line :
Code:
cat souce_file | sed '$d' > dest_file

to copy except last two lines :
Code:
cat souce_file | sed '$d' |sed '$d'  > dest_file

You get a Useless Use of Cat Award. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying few lines from one file to another

Hi all I have a requirement I need to copy only first 2 lines from a file of one location to another file of same location I used to code as cp head -2 abc 123 But i get the following error cp: 0653-437 123 is not a directory. The files used are cat abc ABCD EFDG TYUI (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

2. Shell Programming and Scripting

How to grep on unique id which has request and response on different lines?

Hi I want to find out those unique uids from the log file which have request and response. The log file format is as follows. This log has other irrelevant lines too but each uid should have request and reponse, I need those uids only 2013-04-03 10:51:01,808 INFO <?xml version="1.0"... (4 Replies)
Discussion started by: random_thoughts
4 Replies

3. Shell Programming and Scripting

Copying lines between two strings

Hello All, I want to copy some lines from one file to other with following condition. Only lines between two specified strings should copy. Example :- "My First String " Some_Other_String .... Some_Other_String .... Some_Other_String .... "My Second String" So only... (5 Replies)
Discussion started by: anand.shah
5 Replies

4. Shell Programming and Scripting

Help with copying lines from different files.

I am new to Linux and have a challenging task. I have 3 data files, and need to do the following: Go to line 31 of file 1, delete it Read 1 line from file 2 and add in place of deleted line Go to line 97 of file I delete it and then read the line 1 from file 2 and add in place of that... (2 Replies)
Discussion started by: hamnsan
2 Replies

5. Shell Programming and Scripting

Copying lines from one file to another file

I did a search before posting and couldn't find an example of what I need done. Anyway, I have a file called file1.txt, which has data like this: File2.txt contains: So what I need to do is from file1.txt find the first line M977 in file2.txt and replace the line below it with the line from... (4 Replies)
Discussion started by: Fly_Moe
4 Replies

6. Shell Programming and Scripting

how to create one newfile and wants include some lines based on user request

I have one request, First it has to create one file and in that file it has to include some lines based on user request. Suppose user requested 10, then script needs to add 10 lines like below QAEVO_A1|A1 QAEVO_A2|A2 QAEVO_A3|A3 QAEVO_A4|A4 QAEVO_A5|A5 QAEVO_A6|A6 QAEVO_A7|A7... (8 Replies)
Discussion started by: sridhusha
8 Replies

7. Shell Programming and Scripting

Copying lines from fileA if they start by a word from fileB

Hi I'm in the need of a script that basically takes two files and generates a 3rd. It reads from fileA and fileB and copies lines from fileA if they start by a word of fileB. for example fileA The dog is beautful Where is your cat Why are you sad? Help me! fileB The Where tree dog... (4 Replies)
Discussion started by: FrancoisCN
4 Replies

8. UNIX for Dummies Questions & Answers

Copying common lines to a new file

How do i copy the common lines between 2 files to another file? for example file1: asdfg sdf gdsf file2: asdfgh asd sdf xcv file3: sdf (3 Replies)
Discussion started by: khestoi
3 Replies
Login or Register to Ask a Question