1

I have file that contaian the following lines :

3 5 asd/sdf/rdc fgd/ghw/rtt
4 7 axd/sdf/rdc axd/ghw/ert
8 2 drf/sdf/uhg fgd/ghw/gcd

I want to compare only the first names in coloums 3 & 4 and print if match or not - result should be :

3 5 asd/sdf/rdc fgd/ghw/rtt diff
4 7 axd/sdf/rdc axd/ghw/ert equal
8 2 drf/sdf/uhg fgd/ghw/gcd diff
1
  • Have you tried anything? Commented Sep 20, 2022 at 6:39

2 Answers 2

0

Using awk:

awk '{ split($3,a,"/"); split($4,b,"/"); $(NF+1) = a[1] == b[1] ? "equal" : "diff" }; 1' file

This splits the 3rd and 4th fields on the slashes and then adds a new field depending on whether the first element of the split-up strings are equal or different.

The trailing 1 at the end causes the modified record to be outputted.

0

You can do this with standard sed:

sed 's_$_ diff_;s_\( [^/]*/\)\(.*\1.*\)diff_\1\2equal_'
  • pessimisticly, we always add diff first with s_$_ diff_
  • then we substitute the diff with equal, if the sequence from the whitespace to the next slash [^/]*/ is repeated (\1) later in the line
2
  • great , it's working , thanks you very much Commented Sep 20, 2022 at 9:59
  • @OrenHamama Then please mark the question as solved by accepting one of the answers.
    – Philippos
    Commented Sep 21, 2022 at 6:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .