How do I compare one line to the next.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I compare one line to the next.?
# 1  
Old 07-23-2018
How do I compare one line to the next.?

Q.). How do I take data in columns format and check the second column row to see if the data in the next row but in the second column is the same or not. If it is the same I want to print the full row out and then go to the next row and then do the same thing again but if the second column of the previous row does not match then bring a new line and then print the row and then continue again.


Your help would be greatly appreciated. This has me stumped.

Example:

Code:
Disc    SDB481284    KKFU6958870    true    false    B009.04.32.01   
Disc    SDB480384    KKFU6772437    true    false    B012.02.24.04 +1
Disc    SDB481282    TLLU1065682    true    false    NULL   
Disc    SDB481082    TCLU1330216    true    false    B012.06.12.03   
Disc    SDB480882    SZLU9147417    true    false    B007.05.01.
Disc    SDB471182    AXIU6105411    true    false    B012.08.20.04   
Disc    SDB481382    KKFU6759712    true    false    B007.05.01.02   
Disc    SDB481582    TLLU1038563    true    false    ASC008W
Disc    SDB490682    HLBU9053900    true    true    B008.03.28.03   
Disc    SDB480804    KKFU6578587    true    false    NULL

I want to check to see if SDB48 is the same or not from the second row and if it is different print a new line and then print the that line and so forth.

Output would look like this.

Code:
Disc    SDB481284    KKFU6958870    true    false    B009.04.32.01   
Disc    SDB480384    KKFU6772437    true    false    B012.02.24.04 +1
Disc    SDB481282    TLLU1065682    true    false    NULL   
Disc    SDB481082    TCLU1330216    true    false    B012.06.12.03  
Disc    SDB480882    SZLU9147417    true    false    B007.05.01.

Disc    SDB471182    AXIU6105411    true    false    B012.08.20.04 

Disc    SDB481382    KKFU6759712    true    false    B007.05.01.02   
Disc    SDB481582    TLLU1038563    true    false    ASC008W

Disc    SDB490682    HLBU9053900    true    true    B008.03.28.03  

Disc    SDB480804    KKFU6578587    true    false    NULL


Last edited by vgersh99; 07-23-2018 at 12:43 PM.. Reason: code tags, please!
# 2  
Old 07-23-2018
Welcome tron12,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
  • Do you need to handle a value being repeated later, or just adjacent lines?
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.



Thanks, in advance,
Robin
# 3  
Old 07-23-2018
something along these lines if I undestand your input/output samples correctly:
Code:
awk '{s=substr($2,1,5)} FNR>1 && s!=p {printf ORS }{p=substr($2,1,5)}1' myFile

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 07-23-2018
Hello Robin,

No this is not a homework assignment. I am just wanting to improve on my script to make the displaying of the output a little easier for my co-workers to read.

Below is my script.
Code:
#!/bin/bash



RED='\033[0;31m'
LB='\033[1;34m'
YELLOW='\033[1;33m'
WHITE='\033[1;37m'
GREEN='\033[0;32m'
#DL="/path"
CRANES="105 205 304 609"
DL=/tmp

declare -a myarray
declare -a reefer



echo -n -e "\033]0;Reefer Monitoring Terminal\007"

for i in `seq 1 120`; do

DATE=`date`

clear

echo -e "$YELLOW==================== START OF REEFER LIST --> $DATE ===================="

for i in $CRANES; do

#IFS=""

#if [ -f $DL/DynamicLetter_$i.xml ]; then
#   cat $DL/DynamicLetter_$i.xml | grep Discharge >/dev/null
#   STATUS=$?
#fi
    

if [ -f $DL/DynamicLetter_$i.xml ]; then
   NUM=`cat $DL/DynamicLetter_$i.xml | grep -C 8 '[A-Z0-9][0-9]'RF  | egrep 'Name|Completed|PowerCargo|AutomationLocation|GUID' | awk -F'>' '{ print $2 }' | awk -F'<' '{ print $1 }'  | awk 'NR%2{printf "%s ",$0;next;}1' | wc -l`
else
   NUM=0
fi

 if [ $NUM -gt 0 ]
  then
#  if [ $STATUS -eq 0 ]
#   then
   if [ -f $DL/DynamicLetter_$i.xml ]; then
        index=0
	echo -e "$RED\n*********** Reefers that are completed on$GREEN Crane $i $RED************\n"
	echo -e "$WHITE L/D\tVessel\t\tContainers\tStat\tLive\tLocation\n"

#        myarray=($(cat $DL/DynamicLetter_$i.xml | grep -C 9 '[A-Z0-9][0-9]'RF | egrep 'Name|Completed|PowerCargo|AutomationLocation|GUID' | awk -F'>' '{ print $2 }' | awk -F'<' '{ print $1 }'  | awk 'NR%5{printf "%s\t",$0;next;}1'| tr -s '-' '\t' | sed 's/Discharge/Disc'/g | cut -c 6-10))
        myarray=($(cat $DL/DynamicLetter_$i.xml | grep -C 9 '[A-Z0-9][0-9]'RF | awk -F'>' '{ print $2 }' | awk -F'<' '{ print $1 }'  |  cut -c 6-10))

        reefer=($(cat $DL/DynamicLetter_$i.xml | grep -C 9 '[A-Z0-9][0-9]'RF | egrep 'Name|Completed|PowerCargo|AutomationLocation|GUID' | awk -F'>' '{ print $2 }' | awk -F'<' '{ print $1 }'  | awk 'NR%5{printf "%s\t",$0;next;}1'| tr -s '-' '\t' | sed 's/Discharge/Disc'/g))

        count=${#myarray[@]}
echo "${reefer[0]}"
exit

        while [ "$index" -lt "$count" ]; do

              forward=$index+1

	      if [ "${myarray[$index]}"  ==  "${myarray[$forward]}" ]
	      then
                 printf "${reefer[$index]}" 
                 let "index++"
              else
                 echo -e "\n" 
                 printf "${reefer[$index]}" | awk -F'>' '{ print $2 }' | awk -F'<' '{ print $1 }'  | awk 'NR%5{printf "%s\t",$0;next;}1'| tr -s '-' '\t' | sed 's/Discharge/Disc'/g
                 let "index++"
              fi
         done
	echo -e "$RED\n*********** Reefers that are completed on$GREEN Crane $i $RED************\n"
    fi
   fi
#  fi
done

echo -e "$YELLOW==================== END OF REEFER LIST --> $DATE ======================"

sleep 30
done

# 5  
Old 07-23-2018
In addition to what Robin has already asked, one might note that there are no duplicate values in the second field in your sample input file. Is your real criteria for matching something like:
  • the first 5 characters of the 2nd field,
  • just the 5th character of the 2nd field, or
  • something else that you haven't told us?
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 07-23-2018
That worked great!!! The only thing is that the first iteration 105 it does not separate the data but all the others worked fine 205 304 609

Do you know what might be causing the first one to not go through those rules.

Thank you so much!!

------ Post updated at 10:13 AM ------

Sorry ignore my last comment. It all works great!!!! Thank you!!
# 7  
Old 07-23-2018
So you compare the first 5 characters from the 2nd column.
The awk code in post#3 repeats the substr(), can be shortened p=s.
I think the following is better readable, too:
Code:
awk '(NR > 1 && (s=substr($2,1,5)) != p) {print ""} {p=s; print}'

These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files line by line in bash

Hello All! Thanks for taking time out and helping. My issue is, I have two files that have file names in it. Now, i need to go through each line of both the files and when the file names are different, i need to rename the file. Below is the example: File1</ fil1ename1.txt filename2,txt... (2 Replies)
Discussion started by: svks1985
2 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Compare fields in two files line by line

I am new to awk scripting. I want to do a field by word (field) comparison of two files File1.txt and File2.txt. The files contain a list of | (pipe) separated field. **File 1: ------------------- aaa|bbb|ccc|eee|fff lll|mmm|nnn|ooo|ppp rrr|sss|ttt|uuu|vvv** File 2: ... (7 Replies)
Discussion started by: dhruvmohan
7 Replies

4. Shell Programming and Scripting

Perl how to compare two pdf files line by line

Hi Experts, Would really appreciate if anyone can guide me how to compare two pdf files line by line and report the difference to another file. (3 Replies)
Discussion started by: prasanth_babu
3 Replies

5. Shell Programming and Scripting

how to compare file line by line with awk

im a newbee to unix. I have a requirement to compare two files with awk. file1.txt a b c d e file2.txt a b d e here i want to compare each line in file1 with corresponding line in file2 and prinf the line with difference. ie to check required result as shown below a=a (dont... (3 Replies)
Discussion started by: kiranps
3 Replies

6. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

7. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

8. Shell Programming and Scripting

Compare two text files line by line

Hi all I need help on comparing two texts files line by line and print the total number of lines matched. Any help is appreciated. Thanks in advance. (4 Replies)
Discussion started by: my_Perl
4 Replies

9. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

10. Shell Programming and Scripting

compare data line by line from a file

Hi there How can I compare data line by line from a file? I need to compare the second value with the fourth to know if they are different. If those values are different, I require to send my first value to the output until the complete file has been read. This is my file: 0 FALSE... (1 Reply)
Discussion started by: loperam
1 Replies
Login or Register to Ask a Question