2016-08-29 37 views
0

文件,我已經有(list1.txt):粘貼不能並排返回文件?

test 
for 
you 

文件我做

list2='test2\nfor2\nyou2' 
echo $list2 > list2.txt 

現在,當我嘗試做paste list1.txt list2.txt,它只是返回

test test2 for for2 you you2 

我目標是要返回

test test2 
for for2 
you you2 

這就像它把\ n當作空格,請幫忙。

+0

*現在,當我嘗試做*'粘貼list1.txt list2.txt' ......你怎麼做它?我的印象是,你使用它在一個沒有引號的命令替換,例如'echo $(paste listt1.txt list2.txt)' – Leon

回答

1

你需要告訴echo解釋轉義序列,即做

echo -e "$list2" > list2.txt 

然後,一個很好的格式化輸出,你可以做類似

paste list1.txt list2.txt | expand -t 15 

expand手冊說:

-t,--tabs = LIST 使用逗號分隔的ex列表plicit標籤定位

1

你可以試試這個:

echo -e $list2 > list2.txt 

人呼應

- e 
Enable interpretation of the following backslash-escaped 
characters in each STRING: 

\a   alert (bell) 

\b   backspace 

\c   suppress trailing newline 

\e   escape 

\f   form feed 

\n   new line 

\r   carriage return 

\t   horizontal tab 

\v   vertical tab 

\\   backslash 
0

您可以使用:

# Use $'...' notation to expand backslash-escaped characters 
list2=$'test2\nfor2\nyou2' 
echo "$list2" 
test2 
for2 
you2 

# make sure to use quoted variable 
$> echo "$list2" > list2.txt 

$> paste list1.txt list2.txt 

test test2 
for  for2 
you  you2 
0

list2.txt文件看一看:

$ cat list2.txt 
test2\nfor2\nyou2 

echo命令不會將\n解釋爲換行符。對於這一點,你必須要麼使用-eecho(不可移植),或使用printf(便攜式):

$ printf "test2\nfor2\nyou2\n" >list2.txt