0

Example list:

goodbye  
goodbye  
hello  
hi  
hi  
hi  
no

Expected output:

goodbye goodbye  
hello  
hi hi hi   
no  

I'd like to print the even the 'unlike' word as well, just on a separate individual line. But any matching on the same line. Can 'tr' be used with a loop?

 #!/bin/bash

 while read line
 do
     if [$var == $var]
     then
        echo $var | tr -s ' '

        else
        echo $var | tr '\n' 

     fi

 done < foo.txt
randomir
  • 17,989
  • 1
  • 40
  • 55
Mindy
  • 21
  • 3
  • Do you need a solution in `bash`, or `sed`/`awk`/etc. are also an option? – randomir Oct 20 '17 at 21:33
  • 1
    What does `I'd like to print the even the 'unlike' word as well` mean? – Ed Morton Oct 20 '17 at 22:02
  • @EdMorton sorry.. meant words that aren't duplicated to be printed as well; just on a separate line. – Mindy Oct 20 '17 at 23:04
  • @randomir i do need it in bash, if possible. i've used sort to sort the words alphabetically, so that they're in order. just needed to figure a way a loop to group the duplicates into one line. all other solutions i've looked at online include removing the duplicates. but i want to keep them and group them. – Mindy Oct 20 '17 at 23:04
  • @Mindy have a look then at the solution in bash below. – randomir Oct 21 '17 at 00:10
  • 1
    What do you mean by "in bash"? Do you mean only using shell builtins (i.e. no sort, sed, grep, tr, awk, etc.) or something else? Why wouldn't you just use awk? – Ed Morton Oct 21 '17 at 01:37

3 Answers3

0

awk one-liner:

awk '{ printf "%s%s",(NR==1? "": (r==$1? FS:ORS)),$0; r=$1 }END{ print "" }' file

The output:

goodbye goodbye
hello
hi hi hi
no
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

using awk

If all the matching words are in consecutive lines (as in your input) then you can use this solution.

$awk 'FNR==1{a=$0; ORS=" "} a!=$0{a=$0; $0=RS $0} END{printf RS}1' file

goodbye goodbye 
hello 
hi hi hi 
no
Rahul Verma
  • 2,946
  • 14
  • 27
0

A simple, pure bash, solution that expands on your attempt:

#!/bin/bash
first=1
while read word; do
    if (( first )); then
        printf "%s" "$word"
        first=0
    else
        if [[ $word == $prev ]]; then
            printf " %s" "$word"
        else
            printf "\n%s" "$word"
        fi
    fi
    prev="$word"
done < foo.txt
echo

For each word read from your input file, we check if it's the same as the previous word printed; if yes - we continue to print in the same line; if no - we break the sequence and print the new word in the new line.

The first word is always printed without any prefix (space/newline), so we handle that special case via the first variable.

The printf command is used instead of echo because it has a nicer control of output (format), but echo -n could have been used too.

randomir
  • 17,989
  • 1
  • 40
  • 55