Advertisement
AntonioVillanueva

Rechercher les lignes répétées. Il ne tient pas compte de la ligne de commentaires

Jun 1st, 2023
1,198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.16 KB | None | 0 0
  1. /*Recherche de lignes répétées sans tenir compte des commentaires  A.Villanueva*/
  2. package main
  3.  
  4. import (
  5.     "bufio"
  6.     "fmt"
  7.     "os"
  8.     "strings"
  9. )
  10.  
  11. /*Renvoie true si l'élément existe dans la liste.*/
  12. func exists(elem string, lista []string) bool {
  13.     if elem == "" || strings.Contains(elem, "/*") {
  14.         return false
  15.     }
  16.     for _, valor := range lista {
  17.         if valor == elem {
  18.             return true
  19.         }
  20.     }
  21.     return false
  22. }
  23. func main() {
  24.     fmt.Println("Recherche de lignes répétées sans tenir compte des commentaires")
  25.     if len(os.Args) < 2 {
  26.  
  27.         fmt.Println("Erreur args  ex:", os.Args[0], " fichier.txt")
  28.         os.Exit(1)
  29.     }
  30.     filename := os.Args[1]
  31.  
  32.     // ouvrir le fichier
  33.     file, err := os.Open(filename)
  34.     if err != nil {
  35.         fmt.Println("Error al abrir el archivo:", err)
  36.         return
  37.     }
  38.     defer file.Close()
  39.  
  40.     lines := []string{} //Créer une liste avec les lignes lues
  41.     line := 0           //ligne actuelle
  42.     scanner := bufio.NewScanner(file)
  43.  
  44.     for scanner.Scan() {
  45.         line++
  46.         text := scanner.Text()
  47.         text = strings.ReplaceAll(text, " ", "")
  48.         if exists(text, lines) {
  49.             fmt.Printf("numéro de ligne %d \t %s \n", line, text)
  50.         }
  51.         lines = append(lines, text)
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement