tags:

views:

104

answers:

2

Hi,

I am trying to write a script which parses the fields in the program and prints them to standard output.

Input file data is like this

field1 field2

Fields are separated by tabs

Initially, I tried to write it in AWK.

cat test.txt | while read line; do entity=`awk -F "\t" '{print $2}'`;echo $entity; done

This works fine on command line.

But when I try to do the same in a shell program as below

while read line
do
        entity=`echo $line|awk -F \t '{print $2}'`
        echo $entity
done<"test.txt"

Reason being echo $line actually prints tabs as spaces. I cannot uses spaces because fields can actually contain spaces.

Then I tried to write using cut.

while read line
do
        entity=`echo $line|cut -f5`
        echo $entity
done<"test.txt"

Again same problem with echo.

Have anyone faced similar issue. Any solution that you can recommend me.

Thanks. Bala

+4  A: 

When you have a variable with tabs and then try to echo the variable:

line=$'f1\tf2\tf3'
echo ${line}

What is happening is that the shell is treating the tabs as argument separators and giving echo multiple arguments. echo itself never sees the tabs

If you want to preserve the tabs, you need to see the line as a single argument. In order to do that and still expand the variable, you need to surround the variable with double-quotes:

echo "${line}"

This will now send a single argument that has tabs in it.

This means you should write your script like this:

while read line; do
    entity=$(echo "$line" | awk -F \t '{print $2}')
    echo $entity
done < test.txt
R Samuel Klatchko
Actually I tried this, but only after -r option to read it worked..As the text has to be raw.
Algorist
+1  A: 

with limited info, try this

while read -r line
do
        entity=$(echo "$line"|cut -f5)
        echo "$entity"
done<"test.txt"
ghostdog74
This worked for me..thanks.
Algorist