Score:0

How to read line by line and match text with next line?

cn flag

read the files data line by line and match the line with the next line, if it matches then save that line in the uniqe.txt file.

bac0n avatar
cn flag
sounds like `uniq -d input.txt output.txt` (only print duplicate lines, one for each group)
pasman pasmański avatar
mx flag
What is your Ubuntu version/release ?
bac0n avatar
cn flag
so if you were to have `Abcde x 3`, should result in two `Abcde` in unique.txt?
Score:1
cn flag

You can accomplish this with a simple for-loop

#!/bin/bash

mapfile -t < text.txt
for ((a=0,b=1; $b<${#MAPFILE[@]}; a++,b++)); do
     [[ ${MAPFILE[$a]} = ${MAPFILE[$b]} ]] && echo ${MAPFILE[$a]}
done > unique.txt
Score:0
cn flag

Not sure if "successive duplicate lines" is a key issue for you. If not then you simply need the Linux command uniq to eliminate duplicate lines in the file with:

uniq -u inputfile.txt > uniqe.txt

If, however, you are only interested in eliminating successive duplicates you can use awk:

awk 'NR == 1 {a=$0; print}  a!=$0 {a=$0; print}' inputfile > uniqe.txt
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.