LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   synchronize contents two files with a bash script (https://www.linuxquestions.org/questions/linux-software-2/synchronize-contents-two-files-with-a-bash-script-463818/)

paul_mat 07-13-2006 07:06 PM

synchronize contents two files with a bash script
 
hi there,

What i'm trying to do is have this

nano file1

line1
line2
line3
line6

nano file2

line2
line3
line4
line5

compare the two files (some command file2 & file1)

and then output a third file,

nano file3

line1
line2
line3
line4
line5
line6

with all the contents of the first two files without making a copy of any of the information.

I want to synchronize content of the two files without making duplicate entries

Tinkster 07-13-2006 07:28 PM

cat file1 file2 | sort -u file3

is a quick solution.


Cheers,
Tink

penguintutor 07-14-2006 04:22 AM

Tinkster's entry is missing a redirect sign (perhaps dropped from the formatting).

I believe Tinkster meant to put:
Code:

cat file1 file2 | sort -u >file3
This assumes that the entries are to be sorted alphabetically.

Tinkster 07-14-2006 05:20 AM

I did indeed - thanks for the corrective feed-back :}

A more complex solution that doesn't sort the results be easily accomplished
using awk (it's non-deterministic, though):
[code]
awk '{entries[$0]=$0} END{ for (i in entries) print entries[i]}' file1 file2 > file3
[code]


Cheers,
Tink

analogdigit 01-16-2007 05:29 PM

just one thing !
 
Hi people !!!

I have one thing that I want to do but I just cant find a righr anwser!

I have teo files

file1 ->
101-140664-200612-9000402870
101-140665-200612-9000402861
101-140666-200612-9000402825
101-140667-200612-9000402966
101-140669-200612-9000402856
101-140670-200612-9000408831
101-140671-200612-9000402897
101-140672-200612-9000402875
101-140673-200612-9000402923
101-140674-200612-9000408789
101-140675-200612-9000402900
101-140676-200612-9000402830
101-140677-200612-9000402906
101-140678-200612-9000408038
101-140679-200612-9000402855

and file2 ->
101-140674-200612-9000408789
101-140675-200612-9000402900
101-140676-200612-9000402830
101-140677-200612-9000402906
101-140678-200612-9000408038
101-140679-200612-9000402855
202-248385-200612-9000161331
202-248386-200611-9000066570
202-248386-200611-9000066571
202-248386-200612-9000161314
202-248386-200612-9000411349
202-248387-200611-9000075961
202-248387-200612-9000161303
202-248388-200611-9000076355
202-248388-200612-9000161316
I would like to make a file3, wich will have all lines from file 1 but only if that lines are not in file 2

Is something like that possibile to do from the bash or i need some programing language ?

Please help me !
thanks !

analogdigit 01-17-2007 06:31 AM

on the way
 
I have done this by now :
-----------------------------------------------------------
#!/bin/bash
unixfiles=` cat /program/unixfiles.txt`
files=` cat /program/files.txt`

for i in $unixfiles; do
echo $i
for fa in $files; do
echo "UN, $fa"
if [ "$i" = "$fa" ]; then
echo "same" >> /program/state.txt
else
# cat "$fa" >> /program/samefiles.txt
echo "not same" 1>> /program/state.txt
fi
done
done
exit
------------------------------------------------------------
I think that it is working but i don't know why i cant use
cat "$fa" >> /program/samefiles.txt
if i use that comand then i get some error message !
Does somebody know why ???


All times are GMT -5. The time now is 04:08 AM.