LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 09-15-2014, 02:39 PM   #1
tlegend33
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Rep: Reputation: Disabled
Nested while loop - similar strings


I have two files:

hostname.out - which contains a list of host names
host_info.out - which contains some similar host names to hostname.out and some additional host names. These host names however are listed with FQDN.

What I'd like to do is generate a list of all hosts in hostname.out, then just those hosts from host_info.out that don't exist in hostname.out. I want to skip host names that are similar between the two files. Here's what I came up with but my syntax is incorrect:

Code:
cat hostname.out > host_pwd.out


while read line2
do
   while read line
   do
   if [[ "$line" =~ "$line2" ]]; then
   :
   else
   echo $line2 > host_pwd.out
   done < hostname.out
done < host_info.out
Any help would be appreciated.

Thanks.
 
Old 09-15-2014, 06:16 PM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
A couple of points on your code.
Quote:
echo $line2 > host_pwd.out
It is best to quote variables when using echo.
You probably want to use the >> redirection, so that lines are appended to the host_pwd.out file.
Also, you failed to add 'fi' to the end of the 'if .. else' block.

Perhaps this will achieve what you want.
Code:
cp hostname.out host_pwd.out

while read line; do
  if ! grep -q "$line" host_info.out ; then
    echo "$line" >> host_pwd.out
  fi
done < hostname.out

Last edited by allend; 09-15-2014 at 07:03 PM.
 
Old 09-16-2014, 12:24 AM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
I have had another think about this and perhaps this is more like what you want.
Code:
cp hostname.out host_pwd.out

while read line2; do
  flag=0
  while read line; do
    if (( ${#line} > 1 )) && [[ "$line2" =~ "$line" ]] ; then
      flag=1
      break
    fi
  done < hostname.out
  if (( $flag == 0 )); then
    echo "$line2" >> host_pwd.out
  fi
done < host_info.out

Last edited by allend; 09-16-2014 at 07:57 AM.
 
Old 09-17-2014, 09:36 AM   #4
tlegend33
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Original Poster
Rep: Reputation: Disabled
Thanks for getting back to me. Also, thanks for pointing out the fi and >> portions. I clearly have rocks in my head at the end of the day. At any rate, the code you provided is still giving me every row from both files.

A simplistic example:

Code:
line=server1.internet.com
line2=server1-h1.internet.com

if [[ "$line2" =~ "$line" ]] ; then
      flag=1
echo $flag
      break
else
echo "not working"
    fi

not working
There's something up with the comparison of the two strings.
 
Old 09-17-2014, 06:04 PM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
If you want that the presence of "server1.internet.com" should not cause "server1-h1.internet.com" to appear in the output file then perhaps this will help.
Code:
if [[ "$line2" =~ "${line%%.*}" ]] ; then
The =~ binary operator uses the right hand side as a pattern to be matched in the left hand side. You can manipulate the pattern to be matched using bash parameter expansions as in the example shown above.
You can also use grep style regular expression syntax such as ^ for start of line, $ for end of line and . for character in the pattern.
So,
Code:
if [[ "$line2" =~ ^"${line%%.*}" ]] ; then
will force the match to occur only at the start of $line2
 
  


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
[SOLVED] infinite nested while loop wolverene13 Programming 3 11-14-2012 08:32 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
[SOLVED] for loop and nested find kez1985 Linux - Newbie 1 10-01-2010 10:46 AM
nested loop in Perl help needed Grafbak Programming 9 12-19-2006 10:28 AM
Nested-double loop error Harry Seldon Programming 3 05-06-2006 05:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:56 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
Open Source Consulting | Domain Registration