I'm reading a text file line by line in a bash script. The text file I'm reading is a tab separated csv - however when I try to cut the read line, it does not work, it seems like the \t is converted to a blank space somewhere
Below code is not what I am doing finally - I have not yet implemented the actual workload to the code, until the data can be read reliably.
for (( currlineno=2 ; $currlineno <= $maxlines ; currlineno++ )); do
currline=$(sed -n "$currlineno"p "$IMPORT_TABLE".csv )
echo $currline |cut -f2
done
now when I change the two lines like below it works
for (( currlineno=2 ; $currlineno <= $maxlines ; currlineno++ )); do
currline=$(sed -n "$currlineno"p "$IMPORT_TABLE".csv |tr '\t' ';')
echo $currline |cut -f2 -d ';'
done
but I cannot do it like that as my text file also contains ';' ',' and '.' in the fields. Tab is the only acceptable option for me, as my fields will never contain it.
That's because you don't double quote your variable.
tabbed=$'a\tb'
echo $tabbed : "$tabbed"
When bash sees the variable outside of quotes, it applies word splitting on its contents, and echo
just outputs its parameters separated by spaces. Double quotes make the value one parameter, even if it contains whitespace, newlines, etc.