LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-16-2014, 05:37 PM   #1
Atterus
LQ Newbie
 
Registered: Apr 2014
Distribution: Redhat 6.9
Posts: 13

Rep: Reputation: Disabled
Hello! And also trying to make "matching" script that compares two variables/files


Hello!

I'm relatively new to Linux as a whole and especially this forum I use Linux csh scripts primarily in research applications since it is so powerful and flexible. Anyways, I am having trouble getting a csh script to behave where I have two variables (these can be made into files if needed) that comprise of numbers:

file1:
1
2
3
4
5

file2:
1 564
2 2345
8 23
3 6772
4 2
5 1.45
6 24

What I am trying to do is make it so that when the numbers in the first column match up, it prints out the entire line in file2. I've seen lots of threads on simply getting the first column to work, but I can't figure out getting the following kind of result:

res_file:
1 564
2 2345
3 6772
4 2
5 1.45

Removing the file2 lines not corresponding to file1. I've used alot of the suggestions from here in the past, looking forward to actually being in the community! Thanks ahead of time.
 
Old 04-16-2014, 06:02 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,121

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
i think you can use awk for this:
Code:
[schneidz@hyper ~]$ head atterus-*
==> atterus-1.txt <==
1
2
3
4
5

==> atterus-2.txt <==
1 564
2 2345
8 23
3 6772
4 2
5 1.45
6 24
[schneidz@hyper ~]$ for num in `cat atterus-1.txt`
do 
 awk -v n=$num '$1 == n {print $0}' atterus-2.txt 
done
1 564
2 2345
3 6772
4 2
5 1.45
 
Old 04-16-2014, 06:30 PM   #3
Atterus
LQ Newbie
 
Registered: Apr 2014
Distribution: Redhat 6.9
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks! I will try this.
 
Old 04-16-2014, 11:54 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,508

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
awk can actually perform the entire task:
Code:
awk 'NR==FNR{a[$1];next}$1 in a' file1 file2
 
1 members found this post helpful.
Old 04-17-2014, 02:04 PM   #5
Atterus
LQ Newbie
 
Registered: Apr 2014
Distribution: Redhat 6.9
Posts: 13

Original Poster
Rep: Reputation: Disabled
@schneidz: Thanks for the answer, unfortunately I don't think that my computer is able to use that particular code. I've seen for loop solutions in the past that my computer doesn't seem to recognize, it just crashed with a bunch of errors and claims it can't do the "for" command. It's frustrating for sure since I'm used to for loops on other programs.

@grail: Worked like a charm! Thanks!

Thanks you two! Very prompt as I would expect from this community!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to make several "renames" by inoking a single command? Make a shell script file? Foxbat1155 Linux - Newbie 20 11-25-2011 09:13 AM
[SOLVED] Do I have to "Unset" the variables used in a script ? lithos Linux - Newbie 2 09-27-2011 05:00 PM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 10:01 PM
[SOLVED] How to get "case" to overwrite preset variables in a bash script Mogget Programming 4 02-24-2009 01:19 PM
Script: how to assign values to variables using "awk" results? JZL240I-U Linux - Software 3 11-18-2008 12:59 AM


All times are GMT -5. The time now is 07:44 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration