replace several lines in a file with other lines in another file if condition
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
replace several lines in a file with other lines in another file if condition
Hi,
I have this massive table file with some data in it and I want to replace some lines that are wrong with the correct ones that are in another table file of the same format. The wrong lines are not all together in a block but randomly distributed so I need to make a loop checking if the line is in the other file and if it is, replace it. I want to try and do it with sed or awk but I don't really know how to....
Can anyone help me out please?
Hi,
I have this massive table file with some data in it and I want to replace some lines that are wrong with the correct ones that are in another table file of the same format. The wrong lines are not all together in a block but randomly distributed so I need to make a loop checking if the line is in the other file and if it is, replace it. I want to try and do it with sed or awk but I don't really know how to....
Can anyone help me out please?
Cheers
Yara
Sure, we can help you. You'll have to do two things:
- Post what you've written so far, and tell us where you're having a problem.
- Give us a sample of the data you HAVE and tell us what you WANT
I doubt anyone is going to write a script for you, though, but we'll all be glad to help you debug what you've got.
My table is not THAT massive but it has many columns (53) and lines (1014) and I want to take anther table of the same number and format of columns but with fewer lines (just 137) and put those lines in my first table (replacing some lines), how will I know which lines I want to replace? well, they share the same name in column 1 (e.g. cl1018___m05_fit_s08_1_stamp_4861) but the other numbers (i.e. the other columns) are different so I want to just remove all the old line and put the new one.
I don't have a bash script written for this, I was trying in IDL but it's not working. I hope it's doable in a few lines, if it is not trivial that's, ok, I'll find another way
Thanks a lot,
Yara
Example of the 1st 3 lines of one of the tables (they are both looking the same but one has more lines than the other). I've highlited the beggining of the lines (1st column):
I want to take anther table of the same number and format of columns but with fewer lines (just 137) and put those lines in my first table (replacing some lines)
So Yara, you are inserting *ALL* of the 137 lines into the larger table, if I understand you.
Quote:
... I want to just remove all the old line and put the new one.
Are you actually deleting the lines that begin with cl1018___m05_fit_s08_1_stamp_4861? I can't tell if you are replacing values in those lines, or wiping them out and inserting the other 137 lines as replacements.
The lines you posted tell us what you start with, but as TBOne and H_TeXMeX_H said, what do you *WANT* as a result?
OK sorry for not explaing myself correctly, it's a line replacement what I'm looking for
I want to read the 137 lines on my file2 into file1, look for the equivalent ones in file 1 and replace those lines witht he ones in file2 but in a way that it replaces lines in file 2.
SO I want to take the lines in file 2 and replace the equivalent ones in file1 (the ones with the same name - i.e. value in Column1) to get this output: output_file:
name1 1 1 4 5 6 7 8....
name2 2 2 6 7 8 9 0.... name3 0 0 1 1 1 4 4....
name4 4 4 5 6 1 2 3.... name5 0 3 4 4 2 2 1....
OK sorry for not explaing myself correctly, it's a line replacement what I'm looking for
I want to read the 137 lines on my file2 into file1, look for the equivalent ones in file 1 and replace those lines witht he ones in file2 but in a way that it replaces lines in file 2.
SO I want to take the lines in file 2 and replace the equivalent ones in file1 (the ones with the same name - i.e. value in Column1) to get this output: output_file:
name1 1 1 4 5 6 7 8....
name2 2 2 6 7 8 9 0.... name3 0 0 1 1 1 4 4....
name4 4 4 5 6 1 2 3.... name5 0 3 4 4 2 2 1....
(I've highlighted the changes in file1)
Can you see my point?
Thank you VERY much for your patience!
Yara
No problem...we get it now. So, how about posting what you've written so far, and where you're getting stuck??
We'll be glad to HELP you, but I doubt that anyone is going to write your script FOR YOU. If you don't know about bash programming, there are lots of tutorials on the web (Google has lots of links), that can help you.
If I had to write this, I'd look at the sort and uniq utilities....read their man pages.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.