Problemas al escribir la salida en un archivo
Frecuentes
Visto 29 equipos
0
I am reading in a small csv file in the format size,name
- one set per line. For my testing file I have two lines in the csv file.
Si uso el código
while
IFS=',' read -r size name
do
printf "%s\n" "name"
done < temp1.txt
El name
values for each of the lines is printed to the terminal.
Si uso el código
while
IFS=',' read -r size name
do
printf "%s\n" "name" > temp2.txt
done < temp1.txt
Then only the last name
is printed to the temp2.txt
archivo.
¡¿Qué estoy haciendo mal?!
1 Respuestas
0
Está utilizando >
, so that the file gets truncated every time. Instead, use >>
para anexar:
Entonces debería ser así:
printf "%s\n" "name" >> temp2.txt
^^
Todos juntos:
while
IFS=',' read -r size name
do
printf "%s\n" "name" >> temp2.txt
done < temp1.txt
Ejemplo básico:
$ echo "hello" > a
$ echo "bye" > a
$ cat a
bye # just last line gets written
$ echo "hello" >> a
$ echo "bye" >> a
$ cat a
hello
bye # everything gets written
contestado el 28 de mayo de 14 a las 11:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas file unix while-loop ifs or haz tu propia pregunta.
Thanks, I feel stupid for that. It wasn't quite as simple though, so I had to add in
rm
commands to clear the directory up as it works. - Cocinero DustinGood to read that! Don't blame you for the error, it is quite common and happens to the best of us :) - fedorqui