0

There is a text file that has multiple lines as shown below:

djfldfjd:lisa
ldj:roma
dkjdkjflddlsdkjdlkj:kathy
ldadkjfdsldkjdlkfjdlkkkkkkkkkkkkkkkkkkkkl:purple
dkjdljl:christine
kdllkldkjhhhhhhhhhhhhhhh:george
ldkjfdl:kathy

Now all I need is a script to process all the lines of the above text file and compare the string present after colon and get the repeated string after colon and print those strings before colon.

So I tried with below script, but getting error mentioned below:

#!/usr/bin/sh

input="test.txt"

cat $input | while read line; do output= $(echo $line | cut -d":" -f2); done

for (( i = 0 ; i < "${#output[@]}" ; i++ )) { echo ${output[i]} }

Error message: ./compare.sh: line 11: test1: command not found

Any help would be highly appreciated. Thank you.

2 Answers2

2

Your script is not using sh syntax. If anything that looks more like a mix of ksh and zsh syntax. For basic syntax check, you may want to use shellcheck (available online or as a standalone utility you can install on your system).

But in any case to process text, you don't want to use shell loops.

You'd rather use a text processing tool. Here, for text formatted in fields with fixed delimiter, the obvious one would be awk:

#! /usr/bin/sh -
input=test.txt
awk -F: '
  second_pass {if (count[$2] > 1) print $1; next}
              {count[$2]++}' "$input" second_pass=1 "$input"
0

Got it, so my new addition is:

awk -F: 'second_pass {if (count[$2] > 1) print NR-1 "," $1; next} {count[$2]++}' "$input" second_pass=1 "$input"

It works! Cheers!!

muru
  • 72,889