HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Script help
Operating System - Linux
1827845
Members
1258
Online
109969
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2004 07:10 PM
07-12-2004 07:10 PM
I need your help!!!
I need to know duplicated strings in two files.
One file has 1000line or so, and the other has 550 lines too.
The output that I want is printing the same string, so I will update new string.
For example One file has strings like
webbbs
ryujin
hanbangapple
yumso
sagua
nojuck
ryujin
gajossal
scfarm
kyulnara
dearpia
cwpodofarm
chungma
mealon
samyu...
The Other
andonghoney
ansungfarm
apeace
appletop
apsanjayoun
bawoofarm
bitgolfarm
hanbangapple
bonghwangfarm
celesti
chuksukfarm
chungmaewon
chungpoongfarm
chunmafarm
dearpia
...
so as you see hanbangapple and dearpia are both in two files
I could do know duplicated strings are hanbangapple , dearpia and I can erase them one of files.
what is the script for fit it? any tips..
I need to know duplicated strings in two files.
One file has 1000line or so, and the other has 550 lines too.
The output that I want is printing the same string, so I will update new string.
For example One file has strings like
webbbs
ryujin
hanbangapple
yumso
sagua
nojuck
ryujin
gajossal
scfarm
kyulnara
dearpia
cwpodofarm
chungma
mealon
samyu...
The Other
andonghoney
ansungfarm
apeace
appletop
apsanjayoun
bawoofarm
bitgolfarm
hanbangapple
bonghwangfarm
celesti
chuksukfarm
chungmaewon
chungpoongfarm
chunmafarm
dearpia
...
so as you see hanbangapple and dearpia are both in two files
I could do know duplicated strings are hanbangapple , dearpia and I can erase them one of files.
what is the script for fit it? any tips..
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2004 08:00 PM
07-12-2004 08:00 PM
Solution
So you want to remove words which are duplicated in the files from one of the files.
Using something like:
cat file1 file2 | sort | uniq -d
to list the same words.
Then use sed or awk or your favourite text manipulation tool to remove it.. i.e.
for WORD in $(cat file1 file2 | sort | uniq -d)
do
sed -e "/${WORD}/d" < file1 > file1.out
mv file1.out file1
done
or some such..
Using something like:
cat file1 file2 | sort | uniq -d
to list the same words.
Then use sed or awk or your favourite text manipulation tool to remove it.. i.e.
for WORD in $(cat file1 file2 | sort | uniq -d)
do
sed -e "/${WORD}/d" < file1 > file1.out
mv file1.out file1
done
or some such..
One long-haired git at your service...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2004 08:38 PM
07-12-2004 08:38 PM
Re: Script help
Hi,
If you have enought memory (i think it is possible because the length of files is small), you can store all lines from the file with no modifications in an array, then with awk read the other file and write out this lines that are not in the array.
For example:
You can do
awk -f script.awk file1 file2 > file3
where file3 is the file2 without the strings that are in file1
---- script.awk -----------
BEGIN {
flag_file1=1
filename=" "
}
{
if (filename==" ")
filename=FILENAME
if (filename!=FILENAME)
flag_file1=0
if (flag_file1==1) {
a[NR]=$0
num_lin=NR
} else {
exists=0
for (i=1;i<=num_lin;i++) {
if ($0==a[i])
exists=1
}
if (exists==0)
print
}
}
------------- end script -------------
Cheers.
Frank.
If you have enought memory (i think it is possible because the length of files is small), you can store all lines from the file with no modifications in an array, then with awk read the other file and write out this lines that are not in the array.
For example:
You can do
awk -f script.awk file1 file2 > file3
where file3 is the file2 without the strings that are in file1
---- script.awk -----------
BEGIN {
flag_file1=1
filename=" "
}
{
if (filename==" ")
filename=FILENAME
if (filename!=FILENAME)
flag_file1=0
if (flag_file1==1) {
a[NR]=$0
num_lin=NR
} else {
exists=0
for (i=1;i<=num_lin;i++) {
if ($0==a[i])
exists=1
}
if (exists==0)
}
}
------------- end script -------------
Cheers.
Frank.
Linux?. Yes, of course.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2004 09:01 PM
07-12-2004 09:01 PM
Re: Script help
hai,
use grep to do this. Get one file whose line is less than another. Get line by line and grep that in two files. end, input file will be modified.
#!/usr/bin/ksh
# forum.ksh
set -x
file1=$1
file2=$2
input=""
newfile=/tmp/stringcheck.log
# Remove file if exists
cp -p $newfile
touch $newfile
if [[ $(cat $file1 | wc -l) -lt $(cat $file2 | wc -l) ]]; then
input=$file1
else
input=$file2
fi
while read line; do
grep -q $line $file1 $file2
if [[ $? -eq 0 ]]; then
echo "$line is in $file1 and $file2"
else
echo "$line" >> $newfile
fi
done < $input
# To make the file without same string
cp $newfile $input
## end ###
Regards,
Muthukumar
use grep to do this. Get one file whose line is less than another. Get line by line and grep that in two files. end, input file will be modified.
#!/usr/bin/ksh
# forum.ksh
set -x
file1=$1
file2=$2
input=""
newfile=/tmp/stringcheck.log
# Remove file if exists
cp -p $newfile
touch $newfile
if [[ $(cat $file1 | wc -l) -lt $(cat $file2 | wc -l) ]]; then
input=$file1
else
input=$file2
fi
while read line; do
grep -q $line $file1 $file2
if [[ $? -eq 0 ]]; then
echo "$line is in $file1 and $file2"
else
echo "$line" >> $newfile
fi
done < $input
# To make the file without same string
cp $newfile $input
## end ###
Regards,
Muthukumar
Easy to suggest when don't know about the problem!
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Support
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP