0

I have this file:

http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/I_Cuestionario_general_estimaciones_endireh2016.xlsx
http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/IV_Ingresos_y_recursos_estimaciones_endireh2016.xlsx
http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/VI_ambito_escolar_estimaciones_endireh2016.xlsx
http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/VII_ambito_laboral_estimaciones_endireh2016.xlsx
http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/VIII_ambito_comunitario_estimaciones_endireh2016.xlsx
http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/IX_Atencion_Obstetrica_estimaciones_endireh2016.xlsx
http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/X_ambito_familiar_estimaciones_endireh2016.xlsx

And this bash script:

while read p; do
        echo  "\"$p\""
done < file.txt

I would expect the same file but with double quotes around each line, but this is what bash is outputting:

"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/I_Cuestionario_general_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/IV_Ingresos_y_recursos_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/VI_ambito_escolar_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/VII_ambito_laboral_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/VIII_ambito_comunitario_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/IX_Atencion_Obstetrica_estimaciones_endireh2016.xlsx
"http://www.beta.inegi.org.mx/contenidos/proyectos/enchogares/especiales/endireh/2016/tabulados/X_ambito_familiar_estimaciones_endireh2016.xlsx

Anyone know why bash is behaving this way? And how to output both " double quotes? (beginning and end)

Cyrus
  • 84,225
  • 14
  • 89
  • 153
jake2389
  • 1,166
  • 8
  • 22
  • 1
    it works fine on my machine. what is your environment, like bash version? – delta Aug 24 '17 at 05:04
  • `GNU bash, version 4.3.48(1)-release`. I'm starting to think it's a problem with the input file and the newline chars :/ – jake2389 Aug 24 '17 at 05:07

2 Answers2

3

I'm near certain that the line endings on your input file are CR/LF rather than just LF. This would output:

  • ";
  • the web address;
  • a CR returning the cursor to the beginning of the line;
  • "; and, finally,
  • moving to a new line.

Capture the output to a file and pass it through a dump utility like od -xcb, that should show you the raw bytes being output.

As a test, creating a file consisting of the two lines 123<CR> and 456, I see:

pax> while read p; do echo "\"$p\""; done <testfile
"123
"456"

which seems to indicate the problem is as described.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thanks, I did run it through xxd and apparently `0d` hex was the problem (which is indeed carriage return). I was confused because Sublime Text wasn't able to detect it when I regex searched for `\r`, but I assume it's a bug :/ I fixed it now :) thank you! – jake2389 Aug 24 '17 at 05:16
  • @jake2389 Sublime Text is probably just treating it as a DOS/Windows-format text file, with CR/LF line terminators (which I'm sure is what it is), rather than treating the CR as part of the line (which pure unix programs will generally do). – Gordon Davisson Aug 24 '17 at 05:51
0

If you're having trouble escaping the leading and trailing double quotes, you can just use single quotes around your echo statement. Any double quotes inside of single quotes have no significance in terms of defining a string literal, and vice versa:

while read p; do
        echo '"$p"'
done < file.txt
Zachary Espiritu
  • 937
  • 7
  • 23