Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo changes my tabs to spaces

Tags:

bash

delimiter

I'm taking the following structure from around the net as a basic example of how to read from a file in BASH:

cat inputfile.txt | while read line; do echo $line; done 

My inputfile.txt is tab-delimited, though, and the lines that come out of the above command are space-delimited.

This is causing me problems in my actual application, which is of course more complex than the above: I want to take the line, generate some new stuff based on it, and then output the original line plus the new stuff as extra fields. And the pipeline is going to be complicated enough without a bunch of cut -d ' ' and sed -e 's/ /\t/g' (which wouldn't be safe for tab-delimited data containing spaces anyway).

I've looked at IFS solutions, but they don't seem to help in this case. What I want is an OFS...except that I'm in echo, not awk! I think that if I could just get echo to spit out what I gave it, verbatim, I'd be in good shape. Any thoughts? Thanks!

like image 706
Jenn D. Avatar asked Feb 26 '11 16:02

Jenn D.


People also ask

How do I echo a tab character in Bash?

Use the verbatim keystroke, ^V ( CTRL+V , C-v , whatever). When you type ^V into the terminal (or in most Unix editors), the following character is taken verbatim. You can use this to type a literal tab character inside a string you are echoing.

What is tab in Bash?

One of the most useful features I learned when I first started working with Linux was the “tab completion” feature of Bash. This feature automatically completes unambiguous commands and paths when a user presses the <TAB> key.


1 Answers

Try:

cat inputfile.txt | while read line; do echo "$line"; done 

instead.

In other words, it's not read replacing the tabs, it's echo.

See the following transcript (using <<tab>> where the tabs are):

pax$ echo 'hello<<tab>>there' | while read line ; do echo $line ; done hello there  pax$ echo 'hello<<tab>>there' | while read line ; do echo "$line" ; done hello<<tab>>there 
like image 78
paxdiablo Avatar answered Sep 25 '22 02:09

paxdiablo