Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

shell - 输出到文件时外壳换行的问题(problem with shell new line when output to file)

i have a problem when I want to output several variables to txt file in shell.

(我想将几个变量输出到Shell中的txt文件时遇到问题。)

I want them in one line, but there appear line break after first and third variable.

(我希望它们排成一行,但是在第一个和第三个变量之后会出现换行符。)

I have for example this

(我有这个)

for s in `ls $TESTDIR/results_orig.txt`
    do 
        t_a=(`grep "    a" $s`);
        t_b=(`grep "Bytes written to file: " $s`);
        t_c=(`grep " Total Time:" $s`);
        t_d=(`grep "QP                           " $s`);

        a=${t_a[2]};
        b=${t_b[4]};
        c=${t_c[3]};    
        d=${t_d[2]};

        echo "$a $b $c $d" >> $TESTDIR/results.txt;
    done

I got the variables t_a to t_d are after I parse original txt file to find some values in it.

(在解析原始txt文件以在其中找到一些值之后,我得到了变量t_a至t_d。)

Than I want to find some number ant write them in file without other text that I have in original file.

(比我想找到一些蚂蚁将它们写在文件中,而没有我在原始文件中拥有的其他文本。)

And I want it to write those parsed numbers in one line in new file, like

(我希望它在新文件的一行中写入这些解析后的数字,例如)

13.2 1678 3231.5 2422.1

And I get

(我得到)

13.2 1678 3231.5 2422.1

When I echo any variable separately to the console I get right values.

(当我分别向控制台回显任何变量时,我得到了正确的值。)

So variables are parsed fine.

(因此可以很好地解析变量。)

What could be wrong?

(有什么事吗)

Thank you.

(谢谢。)

  ask by MilosR. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...