0

I have a problem with my script. I tried to read a xml file with cat and read each lines with a loop. For example:

cat file.xml | while read line; do echo $line done

But inside my xml files, i had very long lines without backslash and it seems like cat file.xml didn't take big lines on file. However, when i did cat file.xml without the 'while read line', it works.

Is cat limited by the length of the line? Or did i just do a bad manipulation? What should i do to get these lines?

Thanks and bye.

Here is my script that does not work (in french):

#!/bin/bash

## SCRIPT PERMETTANT DE POUVOIR PRENDRE UNE SOURCE DE TXT POSSEDANT DU TEXTE À CHAQUE LIGNE ET LES PLACER, GRACE À UN MOT CLEF, DANS DES FICHIERS SPECIFIES VIA LE CHEMIN D'ACCESS D'UN FOLDER INDIQUÉ PAR L'UTILISATEUR.

## EXEMPLE ##
## L'utilisateur prend un dossier "X" ou sont contenus des XML. Il a placé dans tous ces XML un mot clé "motclefnumero1". Grace à ce script, il pourra changer ce mot clé par les lignes d'un fichier texte.



#### DEMANDE UTILISATEUR ####
echo 'Quel est le fichier source TXT (Possedant ce que vous voulez mettre)'
read textSource

echo 'Quel est le folder où les fichiers que vous souhaitez traiter sont placés?'
read folderSource

echo 'Indiquer le mot clé souhaité (Exemple : motclef1)'
read motClef


# cat file | cut -c1-80

# TABLEAU CONTENANT LES LIGNES DE NOTRE SOURCE TXT
myArray=()
while IFS= read -r line; do
  myArray+=("$line")
done < "$textSource"

i=0

## PROCESS
ls -1 "$folderSource" | while read file; do
    cat "$folderSource/$file" | while read texte; do
        # Dans le cas où le dossier folderSource n'existe pas
        if [ ! -d "$folderSource/resultat" ]; then
            mkdir "$folderSource/resultat"
        fi

        ## Effectuer la transputation du texte demandé dans notre texte de remplacement
        echo ${texte//$motClef/${myArray[$i]}} >> "$folderSource/resultat/$file"

        echo "Line $i : $texte"

        ## CONSOLE LOG
        echo ${myArray[$i]} $folderSource/$file
        echo $i
    done

    ## Increment i var
    i=$((i+1))
done

RESOLVED : Hello, i've resolved my problem. Instead of use this :

cat "$folderSource/$file" | while read texte; do

Just use IFS to read each line, it works :

while IFS='' read -r texte || [[ -n "$texte" ]]; do
done < "$folderSource/$file"
CaPiTo26
  • 57
  • 2
  • 10
  • You probably don't want to `cat` and pipe regardless, consider [reading this](https://mywiki.wooledge.org/BashFAQ/001) for a somewhat better way, but often if you think you need to read line-by-line you're doing something in an inefficient way, perhaps there's something that could make the job easier if you tell us what you want to do with each line – Eric Renouf Jun 11 '18 at 12:51
  • What exactly does `cat` do in this code which `< "$folderSource/$file" while` doesn't? – AlexP Jun 11 '18 at 13:12
  • What if you `-r` switch with read? – KamilCuk Jun 11 '18 at 13:12
  • I'm trying to get a keyword on my xml files and replace it with something else with 'echo ${text//keyword/newstring}' from a text file. I shared my script above. All worked, exept this only one problem when 'cat' is trying to read long lines. – CaPiTo26 Jun 11 '18 at 13:17
  • Neither `cat` nor Bash has a problem with long lines. Your problem is elsewhere. Your code has some obvious errors; see http://shellcheck.net/ for detailed diagnostics. Perhaps fixing those will also remove the symptom you're seeing. – tripleee Jun 11 '18 at 13:50
  • @YassineBenrazouguiCaPiTo26 if you're trying to replace some text with something else, you should consider a tool like `sed` which is built to do that task and will often be much more fully featured and faster – Eric Renouf Jun 11 '18 at 17:20
  • Hello, i've resolved my problem. Instead of use this : 'cat "$folderSource/$file" | while read texte; do' Juste use IFS to read each line, it works : 'while IFS='' read -r texte || [[ -n "$texte" ]]; do done < "$folderSource/$file"' – CaPiTo26 Jun 12 '18 at 06:52

0 Answers0