开发者

echo removing backslash characters

开发者 https://www.devze.com 2022-12-15 18:47 出处:网络
I am trying to write a script which parses the fields in 开发者_运维问答the program and prints them to standard output.

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


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


with limited info, try this

while read -r line
do
        entity=$(echo "$line"|cut -f5)
        echo "$entity"
done<"test.txt"
0

精彩评论

暂无评论...
验证码 换一张
取 消