LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-13-2006, 07:06 PM   #1
paul_mat
Member
 
Registered: Nov 2004
Location: Townsville, Australia
Distribution: Fedora Core 5, CentOS 4, RHEL 4
Posts: 855

Rep: Reputation: 30
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
 
Old 07-13-2006, 07:28 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
cat file1 file2 | sort -u file3

is a quick solution.


Cheers,
Tink
 
Old 07-14-2006, 04:22 AM   #3
penguintutor
Member
 
Registered: Jun 2006
Location: UK
Distribution: Ubuntu, Mandriva, Redhat and Fedora
Posts: 118

Rep: Reputation: 15
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.
 
Old 07-14-2006, 05:20 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 01-16-2007, 05:29 PM   #5
analogdigit
LQ Newbie
 
Registered: Apr 2005
Posts: 7

Rep: Reputation: 0
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 !
 
Old 01-17-2007, 06:31 AM   #6
analogdigit
LQ Newbie
 
Registered: Apr 2005
Posts: 7

Rep: Reputation: 0
Thumbs down 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 ???
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to compare dir contents Boffy Programming 2 08-02-2005 06:08 AM
Retrieving variables from contents of files using awk or any other bash tool genderbender Programming 8 01-21-2005 03:27 AM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
bash script on manipulating files fiomba Linux - Software 8 10-30-2004 08:31 PM
Identifying files in bash script Cruger Programming 7 03-15-2004 10:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration