Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 18, 2013, 02:49 PM   #1
wrathkeg
macrumors newbie
 
Join Date: Feb 2010
comparing adjacent lines of file

Hi all.

Not sure if this is the right place to ask this, but here goes.

Is there a way, preferably using terminal commands, I can compare adjacent lines of a text file to see if they contain any of the same words?

So for a file like this
Code:
one two three
three four five
six seven eight
the first and second lines get returned (since 'three' is repeated) but not the second and third lines since they don't contain any of the same words.

Thanks.
wrathkeg is offline   0 Reply With Quote
Old Mar 18, 2013, 05:08 PM   #2
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
My first thought was to use grep but that's probably not what you want for this. I guess awk would be better suited for doing such a thing.
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Mar 19, 2013, 04:07 AM   #3
wrathkeg
Thread Starter
macrumors newbie
 
Join Date: Feb 2010
Thanks. To use grep I think I would need to know which string I am looking for in advance, which I don't. I'm also not sure that I could apply grep to particular lines. I'll have a look at awk.
wrathkeg is offline   0 Reply With Quote
Old Mar 19, 2013, 06:22 AM   #4
cqexbesd
macrumors member
 
Join Date: Jun 2009
Not sure the exact semantics you are asking for (i.e. if 3 lines in a row have repeated words does the middle line come out twice, once for each pair?) but something like this might get you started.

Code:
perl -anE 'BEGIN { $prev = []; $, = " "; } foreach $p (@{$prev}) { if ($p ~~ @F) { say("@{$prev}\n@F"); last; }}; $prev = [ @F ]'
Just pipe in the data you want to process.
cqexbesd is offline   0 Reply With Quote
Old Mar 19, 2013, 06:46 AM   #5
wrathkeg
Thread Starter
macrumors newbie
 
Join Date: Feb 2010
thanks for that. I don't know much about Perl, but that certainly looks like a possibility. I have just finished putting together a script which seems to work for my needs so I am posting it here. I am sure that it is not the best way to do it, but seems to do the job. Obviously at a minimum commands could be introduced and altered to avoid the creation of all those temporary files (or at least delete them).
Code:
tail -n +2 $1 > $1-short
# find out how many lines there are to look at
a=($(wc $1-short))
# start a loop to take place as many times as there are lines
for i in $(eval echo {1..$a})
do 
  # output specified line
  sed -n -e "$i"p $1 > $1-single
  sed -n -e "$i"p $1-short > $1-short-single
  # split after every space to make columns
  tr ' ' '\n' < $1-single > $1-single-col
  tr ' ' '\n' < $1-short-single > $1-short-single-col
  # output shared words
  comm -12 <(sort $1-single-col | uniq) <(sort $1-short-single-col | uniq) > output-tmp
  # delete newlines so that empty files are really empty
  tr -d '\n' < output-tmp > output-tmp2
  # check if file is empty (no shared words) and not, send relevant lines to output
  if [[ -s output-tmp2 ]] 
  then
    cat $1-single >> output
    cat $1-short-single >> output
    echo -- >> output
  fi
done
wrathkeg is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Comparing folders. Tech198 OS X Mavericks (10.9) 2 Apr 6, 2014 10:11 AM
Installing Apple TV in an adjacent closet Cgalbreath Apple TV and Home Theater 7 Dec 26, 2013 09:46 AM
Diff tool for comparing text file version in Time Machine? callagga Mac Applications and Mac App Store 1 Jul 9, 2013 07:41 PM
Comparing Two Arrays Ides iPhone/iPad Programming 5 Aug 10, 2012 04:56 PM

Forum Jump

All times are GMT -5. The time now is 07:21 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC