Help with copying lines from different files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with copying lines from different files.
# 1  
Old 02-09-2012
Question 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:
  1. Go to line 31 of file 1, delete it
  2. Read 1 line from file 2 and add in place of deleted line
  3. Go to line 97 of file I delete it and then read the line 1 from file 2 and add in place of that deleted file.
The thing is also important to keep the same file i.e file , it is not to be changed.
I tried different versions of sed and perl, with buffer copying tricks but was not successful. Smilie

I am open for all suggestions and request the experts to give me suggestions.
# 2  
Old 02-09-2012
Well, it's very clunky, but you could try:-
Code:
echo "31Gddk:r !head -1 file2\n:wq"|vi file1
echo "97Gddk:r !head -1 file2\n:wq"|vi file1

Does that do it? I feel a proper sed or awk would be better if the files are large. Additionally, remember the vi has limitation such as maximum line length and space for the temporary files it uses, often in /var

You could also:-
Code:
(head -30 file1;head -1 file2;head -96 file1|tail +32;head -1 file2;tail +98 file1) > outputfile

You would need to check the exact records this catches as head & tail vary accourding to OS version. You also cannot write this back to file1 until you have created the replacement file, so you might hit space issues.



Could you expand on the reasons for the edit and sizes of files etc.



I hope that this helps if it's a small enough file to be processed each time.

Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 02-09-2012 at 12:38 PM.. Reason: Added another option
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 02-09-2012
This leaves all of the old files unchanged, creates 2 new ones with the change
Code:
newline=$(head -1 file2)
awk -v n="$newline" 'NR==31 {print v; next} {print} ' file1 > newfile1
awk -v n="$newline" 'NR==97 {print v; next} {print} ' fileII > newfileII

newfile1 and newfileII are the names of the new files.
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 files

I'm trying to do this exact same thing, so far I have created this to move files i've named my script CP.sh #!/bin/bash cd /root/my-documents/NewDir/ for f in *.doc do cp -v $f root/my-documents/NewDir $f{%.doc} done When i go to run this in the console i type, bin/sh/ CP.sh but it... (7 Replies)
Discussion started by: MKTM_93_SIMP
7 Replies

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

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

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

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

7. Shell Programming and Scripting

Files copying - [ Listed files alone. ] - Shell script

Hi All, I am doing this for svn patch making. I got the list of files to make the patch. I have the list in a file with path of all the files. To Do From Directory : /myproject/MainDir To Directory : /myproject/data List of files need to copy is in the file: /myproject/filesList.txt ... (4 Replies)
Discussion started by: linuxadmin
4 Replies

8. UNIX for Advanced & Expert Users

copying of files by userB, dir & files owned by userA

I am userB and have a dir /temp1 This dir is owned by me. How do I recursively copy files from another users's dir userA? I need to preserve the original user who created files, original group information, original create date, mod date etc. I tried cp -pr /home/userA/* . ... (2 Replies)
Discussion started by: Hangman2
2 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