I'm trying to compare files from folder1 to their updated version in folder2. The files have identical names, but may have different number of strings. The strings look like:

string_1_name: "some text"
string_2_name: "some text"
string_n_name: "some text"

So I need to compare only the parts of the strings before colon - string_1_name, and then if a file1.txt from folder2 has an extra or updated string, I want to copy the full string string_1_name: "some text" and paste it into the file1.txt from folder1 but in the same location (an updated string can be in-between old strings) or replace an old string with an updated one. And then create the result file1.txt (with the same name) in folder3.

It will be great if the newly added strings in the result files were highlighted some how, colored or at least bolded.


  • 1
    Please show an example of a filename in folder1 and its correspondent in folder2, as per how written it's hard to understand. I guess you meant " I need to compare only the parts of the strings after colon"? – kos Jul 13 '15 at 4:30
  • Not exactly, I don't care about "some text", I just care about the first part of a string, before the colon divider. Example of a string: noas_no_fort: "Without a fort, there is nothing to assault." The text in quotes in file1.txt will be in different language, but the name of a localisation will be in both cases in english. – Billy Kubr Jul 13 '15 at 5:30
  • Also, the strings are divided by 'Carriage returns'. – Billy Kubr Jul 13 '15 at 5:36
  • The filenames will be identical in both folders, ex.: file_1_english.txt. In folder2 file_1_english.txt may contain more strings than same file from folder1. – Billy Kubr Jul 13 '15 at 5:42

try diff -u file_1.txt file_2.txt > patch1o2 then patch -p0 < patch1o2

the first command will create a file called patch1o2 containing all the difference between the two files
The second command will add in the first file the content of the difference at the same line position.

output will be a file_1.txt updated with extra lines from file_2.txt

edit 14/07/2015 : create a bash script with a loop to analyse all files present in folder here are an example:

for i in "$FILE_1"
    awk -F"," '{print $1}' "$i"
| improve this answer | |
  • That will work if I wanted to compare a whole file, but I need to compare just the names of the strings, if there are any new strings added. And then copy that added (whole) string to the original file. – Billy Kubr Jul 13 '15 at 5:52
  • then make a diff only on column1 with somethinf like awk -F ":" '{print $1}' – green frog Jul 13 '15 at 5:56
  • How about doing it for all files in those folders? – Billy Kubr Jul 13 '15 at 16:22
  • one thing that I don't understand, why diff -u [...] followed with patch _p0 is not OK. its mean that you could have the same first column in the two files with only the second one which differ ? – green frog Jul 13 '15 at 18:52
  • Because in this case the second column in file_1.txt gets replaced by the second column in file_2.txt – Billy Kubr Jul 13 '15 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.