LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2016, 01:23 PM   #1
g_paschoal
Member
 
Registered: Oct 2009
Posts: 131

Rep: Reputation: 15
beginner shell script help


Hi Guys, I am sysadmin and I know a lot of stuffs but when comes to programming and shell script I get stuck.

I need to write a script that will compare two files in the following way.

file1 = about 1800 lines
file2 = about 700 lines.

I need to check if all lines in file2 is inside of file1

how can I do that ?

I really appreciate your help. Sorry if this is too beginner.

Thanks
 
Old 02-03-2016, 02:07 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,675

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by g_paschoal View Post
Hi Guys, I am sysadmin and I know a lot of stuffs but when comes to programming and shell script I get stuck.
I need to write a script that will compare two files in the following way.

file1 = about 1800 lines
file2 = about 700 lines.

I need to check if all lines in file2 is inside of file1

how can I do that ? I really appreciate your help. Sorry if this is too beginner.
Please, don't lie.

You have been here for SEVEN YEARS at this point, so saying you're a 'beginner' is not true...especially when you follow it up with "I know a lot of stuffs". Especially when you've also been asking about programming for years:
http://www.linuxquestions.org/questi...ss-4175511218/
http://www.linuxquestions.org/questi...me-4175483015/

...and you can go back to 2011, when you asked a question about finding strings in files, and try to apply what you were told:
http://www.linuxquestions.org/questi...-files-875635/

...or you can go to Google, and look up one of the THOUSANDS of easily-found bash scripting tutorials. Read the "Question Guidelines" link in my posting signature.

Since you've been programming for years, you should know that there are many ways to do this, depending on the lines in question, of which you post NO samples. Further, you don't say what language you want to use. You may be able to slurp the whole smaller file into an array in Perl and grep through the other file quickly...or can read one line at a time, and look for it in the other file, and set a flag/exit if you don't find it.

Many ways to do it....so show us what YOU have thought of/done/tried and we can try to help.
 
2 members found this post helpful.
Old 02-03-2016, 02:21 PM   #3
g_paschoal
Member
 
Registered: Oct 2009
Posts: 131

Original Poster
Rep: Reputation: 15
My Friend, I don't know you, but I don't think this is a good way to answer my questions. I do have lots of experience in Linux but NOT in programming. I know how to find strings in a file etc but I am NOT good with programming logic, that's why I posted a question here.

Beside, looks at the date I posted the questions. I haven't done a shell script since 2011. I wasn't good, and now I forgot a lot of things. You CANNOT just assume that because I posted those questions that I am a programmer and I know how to do it.

I HAVE POSTED THIS QUESTIONS BECAUSE I DON'T KNOW HOW TO DO IT, AND BELIEVE ME I HAVE GOOGLED FOR IT A LOT.

If you don't want it's fine, but don't stay in the way. YOu don't know my life enough to say anything.





Quote:
Originally Posted by TB0ne View Post
Please, don't lie.

You have been here for SEVEN YEARS at this point, so saying you're a 'beginner' is not true...especially when you follow it up with "I know a lot of stuffs". Especially when you've also been asking about programming for years:
http://www.linuxquestions.org/questi...ss-4175511218/
http://www.linuxquestions.org/questi...me-4175483015/

...and you can go back to 2011, when you asked a question about finding strings in files, and try to apply what you were told:
http://www.linuxquestions.org/questi...-files-875635/

...or you can go to Google, and look up one of the THOUSANDS of easily-found bash scripting tutorials. Read the "Question Guidelines" link in my posting signature.

Since you've been programming for years, you should know that there are many ways to do this, depending on the lines in question, of which you post NO samples. Further, you don't say what language you want to use. You may be able to slurp the whole smaller file into an array in Perl and grep through the other file quickly...or can read one line at a time, and look for it in the other file, and set a flag/exit if you don't find it.

Many ways to do it....so show us what YOU have thought of/done/tried and we can try to help.
 
Old 02-03-2016, 02:39 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Something like: # Untested.
Code:
for line in `cat file2` ; do grep -v $line file1; done
or use "diff"?

I wonder about what you used at google.

http://mylinuxbook.com/bash-shell-scripting-part-i/
http://mylinuxbook.com/bash-shell-scripting-2/
http://www.linuxquestions.org/questi...eniuses-35795/
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
http://tldp.org/LDP/abs/abs-guide.pdf
http://www.tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls

Good Luck.

Last edited by Habitual; 02-03-2016 at 02:46 PM.
 
2 members found this post helpful.
Old 02-03-2016, 03:10 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,675

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by g_paschoal View Post
My Friend, I don't know you, but I don't think this is a good way to answer my questions. I do have lots of experience in Linux but NOT in programming.
First, I'm NOT your friend. And yet you've been posting programming questions for YEARS now.
Quote:
I know how to find strings in a file etc but I am NOT good with programming logic, that's why I posted a question here.
Really?? Despite posting NUMEROUS programming questions WITH CODE???
Quote:
Beside, looks at the date I posted the questions. I haven't done a shell script since 2011. I wasn't good, and now I forgot a lot of things. You CANNOT just assume that because I posted those questions that I am a programmer and I know how to do it.
No, but since:
  • You have been here for SEVEN YEARS
  • You have asked NUMEROUS programming questions, and provided code
  • You have AMPLE resources to be able to write a bash script on your own
  • You have OBVIOUSLY not tried to look up MANY tutorials and examples you can find on Google for bash scripting
  • You have OBVIOUSLY not tried to look this up on LQ either, since this has been asked (and answered) MANY times here
  • You DID NOT say which language you wanted to do this in
..and most importantly:
  • YOU SHOWED NO EFFORT OF YOUR OWN
Quote:
I HAVE POSTED THIS QUESTIONS BECAUSE I DON'T KNOW HOW TO DO IT, AND BELIEVE ME I HAVE GOOGLED FOR IT A LOT.
No, I don't believe you...since there are THOUSANDS of hits for this.
Quote:
If you don't want it's fine, but don't stay in the way. YOu don't know my life enough to say anything.
I know you're trying to get someone else to do this for you...and I'm happy to help you, but YOU, PERSONALLY have to show some effort of your own.
 
Old 02-03-2016, 03:11 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,675

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by Habitual View Post
Something like: # Untested.
Code:
for line in `cat file2` ; do grep -v $line file1; done
or use "diff"?
You're far more generous than I would have been, given the OP's posting history and apparent lack of effort.
I wondered the EXACT same thing.
 
Old 02-03-2016, 03:36 PM   #7
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Rep: Reputation: Disabled
I don't know what else to do but laugh at you.

yet, I DO NOT KNOW HOW TO DO THIS SHELL SCRIPT. This used to be a very good forum but unfortunatlly people like you are spoiling it.

I won't argue with you any more. I will find a another forum for help.

AGAIN, I DO NOT KNOW HOW TO SOLVE THIS. Yet if I knew I would already have done it myself and wasn't here in this forum loosing my time arguing with you.

AGAIN, you do not know about my life, I started to work with different things not related to computer and now I am back to this kind of job, that's whay I don't remember almost anything, because when you don't practice your forgot stuffs. And I do run into people like you. That's said.

AGAIN, YOU DO NOT HAVE ENOUGH INFORMATION ABOUT ME TO SAY THAT.

I wanted to do with shell script, of course. And I already have google for it a lot.
 
Old 02-03-2016, 03:42 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
You're far more generous than I would have been, given the OP's posting history and apparent lack of effort.

I wondered the EXACT same thing.
In the time it takes me to whip out a for i loop, I may as well have done the script for him.
I'd rather post concepts/techniques
I'd rather lead them to water and most days I agree with the things you write/say.

I must have 1000 links bookmarked for things I know nothing about
I bookmark 'em because I see the value in code even when I am not using the code that I value.

As for google, it practically answers the question(s) as you type them in, so I have to wonder what people "do" at google exactly.

There was this guy once, he posted the Year's Most Horrific Crime Stats for Chicago (way back in the early days, circa ~2000) and he
then hacked the Chicago PD website and overlayed the Crime Stats on the PD's webpage.

The news reported "Chicago PD is looking him, so is Google".

When they found him, they asked how he did it.
He replied I asked google how.

I am motivated by this experience, see new sig.

Last edited by Habitual; 02-03-2016 at 03:44 PM.
 
Old 02-03-2016, 03:49 PM   #9
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Rep: Reputation: Disabled
Yes I did created another user because I DO NOT KNOW HOW TO SOLVE THIS. for god sake


Quote:
Originally Posted by gabrielpasv View Post
I don't know what else to do but laugh at you.

yet, I DO NOT KNOW HOW TO DO THIS SHELL SCRIPT. This used to be a very good forum but unfortunatlly people like you are spoiling it.

I won't argue with you any more. I will find a another forum for help.

AGAIN, I DO NOT KNOW HOW TO SOLVE THIS. Yet if I knew I would already have done it myself and wasn't here in this forum loosing my time arguing with you.

AGAIN, you do not know about my life, I started to work with different things not related to computer and now I am back to this kind of job, that's whay I don't remember almost anything, because when you don't practice your forgot stuffs. And I do run into people like you. That's said.

AGAIN, YOU DO NOT HAVE ENOUGH INFORMATION ABOUT ME TO SAY THAT.

I wanted to do with shell script, of course. And I already have google for it a lot.
 
Old 02-03-2016, 03:55 PM   #10
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
gabrielpasv:

Calm down.
In the amount of time you are taking to write these replies,
you could have written the script.

Keep those bookmarks I gave you and/or see my LQ blog for about three dozen more links.

You're Welcome, btw.

Last edited by Habitual; 02-03-2016 at 03:59 PM.
 
1 members found this post helpful.
Old 02-03-2016, 04:01 PM   #11
gabrielpasv
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Rep: Reputation: Disabled
Hi Habitual, thanks

I think that too. If I knew how to do it.

This worked

Quote:
for i in `cat file1.txt` ; do grep $i file2.txt; done |sort > file3.txt
However it created a file. And what I need to compare, if all the lines of file1.txt belongs to file2.txt then I want to trigger an email alert.

And right now I am just creating another file. So I was thinking maybe about compare the too files after they got created ?

I google for it and I saw the "cmp" command. But I need to check

Thanks for your help any way. You are a good person.




Quote:
Originally Posted by Habitual View Post
gabrielpasv:

Calm down.
In the amount of time you are taking to write these replies,
you could have written the script.

Keep those bookmarks I gave you and/or see my LQ blog for about three dozen more links.
 
Old 02-03-2016, 04:29 PM   #12
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Have a look at the code in https://www.linuxquestions.org/quest...il-4175553535/

Some friendly, time-saving advice?
at Google,
Code:
<search_term> site:linuxquestions.org
Have fun and don't forget to post the solution in this post for the next guy that
uses
Code:
<search_term> site:linuxquestions.org
5 years from now.

It may even be you

Last edited by Habitual; 02-03-2016 at 04:30 PM.
 
1 members found this post helpful.
Old 02-03-2016, 07:01 PM   #13
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,675

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by gabrielpasv View Post
I don't know what else to do but laugh at you.
yet, I DO NOT KNOW HOW TO DO THIS SHELL SCRIPT. This used to be a very good forum but unfortunatlly people like you are spoiling it.
Just like people who ignore the forum rules, and create second user ID's? And again, we will HELP you, so show what effort you have put forth. Asking for things without showing any effort is nothing but asking for a handout. You say you've been a systems administrator for SEVEN YEARS, and have posted numerous programming questions...and you actually expect us to believe that you have NO IDEA how to BEGIN to write a simple shell script???
Quote:
I won't argue with you any more. I will find a another forum for help.
Thank you.
Quote:
AGAIN, I DO NOT KNOW HOW TO SOLVE THIS. Yet if I knew I would already have done it myself and wasn't here in this forum loosing my time arguing with you.
Great...no one is arguing with you. We're asking you to show what effort you have put forth. You have not.
Quote:
AGAIN, you do not know about my life, I started to work with different things not related to computer and now I am back to this kind of job, that's whay I don't remember almost anything, because when you don't practice your forgot stuffs. And I do run into people like you. That's said. AGAIN, YOU DO NOT HAVE ENOUGH INFORMATION ABOUT ME TO SAY THAT.
I only know what you post. And you've posted for seven years, and posted about programming, and have (going on) 200 posts. You should be WELL aware of the forum guidelines (read "Question Guidelines" and "How to ask a smart question" links), about doing basic research first.
Quote:
I wanted to do with shell script, of course.
And how are we supposed to know "of course"? when you don't SAY what you're after? You can do this with a dozen different languages, including (as pointed out) the diff command.
Quote:
And I already have google for it a lot.
Sorry, just don't believe you. Habitual handed you numerous links. Putting "how to find out if all lines in one file are in another in linux" gives 89 MILLION results.

And you honestly expect us to believe you "google for it a lot"????
 
Old 02-03-2016, 07:09 PM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,675

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by Habitual View Post
In the time it takes me to whip out a for i loop, I may as well have done the script for him.
I'd rather post concepts/techniques
I'd rather lead them to water and most days I agree with the things you write/say.
Exactly...but after seven years, how much more leading can one do? And claiming ignorance after posting programming questions is even worse.
Quote:
I must have 1000 links bookmarked for things I know nothing about I bookmark 'em because I see the value in code even when I am not using the code that I value.
ABSOLUTELY...I have a similar list. Someone here posted one the other day that was great, and I have it saved off too.
Quote:
As for google, it practically answers the question(s) as you type them in, so I have to wonder what people "do" at google exactly.

There was this guy once, he posted the Year's Most Horrific Crime Stats for Chicago (way back in the early days, circa ~2000) and he
then hacked the Chicago PD website and overlayed the Crime Stats on the PD's webpage.

The news reported "Chicago PD is looking him, so is Google". When they found him, they asked how he did it. He replied I asked google how. I am motivated by this experience, see new sig.
Yep...that's how I learned almost all the languages I know. I sure didn't ask for handouts, but started looking for ways to do what I wanted, trying them, and learning from those mistakes.

I just don't see how anyone, in 2016, can show no effort of their own, with the amount of information/samples/examples that are literally at their fingertips.

As I've said many times in other threads, I'll gladly help anyone, but I refuse to give handouts. Posting "hey, I'm trying to do xxxx, with these commands, and I'm just not getting the results I think I should, based on yyyy from the man pages. I'm missing something, can anyone help?" is one thing. Someone who has years of experience, and claims to "know lots', and has asked programming questions before, and asks something VERY common with no visible effort? Thanks, but no...
 
Old 02-03-2016, 07:12 PM   #15
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,675

Rep: Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970Reputation: 7970
Quote:
Originally Posted by gabrielpasv View Post
Yes I did created another user because I DO NOT KNOW HOW TO SOLVE THIS. for god sake
Thanks for ignoring the LQ Rules. Stop complaining, and start doing some work, and show some effort.

There are hundreds (if not thousands) of links on this very site that deal with this question. 89 million hits on Google. Not sure how much more guidance/hints/suggestions/samples you need, before you can start to do something on your own.
 
  


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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
LXer: Beginner's Shell Scripting On Linux And Unix: Why And How LXer Syndicated Linux News 0 04-16-2009 03:20 PM
Beginner in shell scripting som_kurian Programming 7 11-14-2007 12:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 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