2016-12-01 194 views
0

我有一个具有以下数据的输入文件:重定向输出到输出文件

one two 
three 
fish hat 
cat hop 

我必须写下面的输出文件

one two three 
fish hat cat hop 

这里是我的代码,我有这么远

#!/bin/bash 
# 

# input and output files 
FILE=$1 
FILE2=$2 

#checks to see if the input file exists 
if [ -f $FILE ];then 
echo "file $FILE found!" 
else 
echo "file $FILE does not exist" 
exit 1 
fi 

#check to see if output file is empty 
if [ -s $FILE2 ]; then 
echo "$FILE2 already has data!" 
exit 1 
fi 

#Joins every two input lines 
while read first; do read second; echo "$firstLine $secondLine"; 
done < $FILE 

cat $FILE &> $FILE2 

我得到的输出文件时,我使用cat命令显示的内容打印到控制台但是输出文件。它显示原始输出文件。有人能指出我犯的错误吗?

+0

用GNU sed和你的例子:'sed'1〜2 {N; s/\ n//}'file' – Cyrus

回答

0

这会做你要求什么:

cat $file | paste -d ' ' - - 

而将输出发送到文件2:

cat $file | paste -d ' ' - - > $file2 

注意:使用小写变量,大写的变量是保留给环境变量。

0
done < $FILE 

cat $FILE &> $FILE2 

我得到当我使用cat命令显示输出文件的内容输出文件打印到控制台但是。它显示原始输出文件。有人能指出我犯的错误吗?

的错误是,你不写输出到输出文件摆在首位,并且即使你这样做,你会覆盖由cat …输出文件;删除后者并将上面的行更改为:

done <$FILE >$FILE2