Copying few lines from one file to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying few lines from one file to another
# 1  
Old 01-22-2014
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
Code:
cp head -2 abc 123

But i get the following error
cp: 0653-437 123 is not a directory.

The files used are
Code:
cat abc
ABCD
EFDG
TYUI

cat 123
1234
1ert
6789
7890

I want the output file as

ABCD
EFGD
6789
7890

Please help!!
# 2  
Old 01-22-2014
use piping
Code:
head -2 abc >a |cp a 123

# 3  
Old 01-22-2014
Refer to man cp for why the command is not working.
In short, If you provide two argument to "cp", it would copy the first file (arg1) to the second file (arg2) but If you provide more then two arg (your case), it expects the last arg to be a directory to copy all the files inside that directory. In this case, "123" is a file hence error.

You can probably use

Code:
for file in abc 123
do
 head -2 $file > new_file
done

# 4  
Old 01-22-2014
I can use a for loop

Instead is there any way in a single command where the copy operation can be done ??
# 5  
Old 01-22-2014
man head, look for -q option.
# 6  
Old 01-22-2014
You are not copying the files, you are modifying them. cp won't work. Try
Code:
head -2q abc 123

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: copying lines with specific character to files with same name as copied line.

I am trying to make my script as simple as a possible but, I am not sure if the way I am approaching is necessarily the most efficient or effective it can be. What I am mainly trying to fix is a for loop to remove a string from the specified files and within this loop I am trying to copy the lines... (2 Replies)
Discussion started by: Allie_gastrator
2 Replies

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

3. Shell Programming and Scripting

Copying lines from multiple logfiles, based on content of the line

d df d d (1 Reply)
Discussion started by: larsk
1 Replies

4. Shell Programming and Scripting

[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! ;) 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... (6 Replies)
Discussion started by: WideMind
6 Replies

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

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

7. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

8. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

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

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