2014-01-24 75 views
1

我有2个文件file1和file2。我正在尝试从file1读取一行,并从file2读取另一行,并插入HTML 标志以使其在html文件中成为usealbe。我一直试图与awk一起工作,但收效甚微。有人可以帮忙吗?在UNIX中一次读取两行文本文件一行

File1中:

SILOS.SIL_Stage_GroupAccountNumberDimension_FinStatementItem 
SDE_ORA11510_Adaptor.SDE_ORA_Stage_GLAccountDimension_FinSubCodes 

文件2:

FlatFileConnection.DBConnection_OLAP.SILOS.SIL_Stage_GroupAccountNumberDimension_FinStatementItem.txt 
FlatFileConnection.DBConnection_OLAP.SDE_ORA11510_Adaptor.SDE_ORA_Stage_GLAccountDimension_FinSubCodes.txt 

希望的输出:

<ParameterFile> 
<workflow>SILOS.SIL_Stage_GroupAccountNumberDimension_FinStatementItem</workflow> 
<File>FlatFileConnection.DBConnection_OLAP.SILOS.SIL_Stage_GroupAccountNumberDimension_FinStatementItem.txt</File> 
<ParameterFile> 
<workflow>SDE_ORA11510_Adaptor.SDE_ORA_Stage_GLAccountDimension_FinSubCodes</workflow> 
<File>FlatFileConnection.DBConnection_OLAP.SDE_ORA11510_Adaptor.SDE_ORA_Stage_GLAccountDimension_FinSubCodes.txt</File> 
+1

_很少成功_ - 它会很高兴地显示你的尝试。用于awk解决方案的 – devnull

回答

2

使用bash:

printItem() { printf "<%s>%s</%s>\n" "$1" "${!1}" "$1"; } 

paste file1 file2 | 
while read workflow File; do 
    echo "<ParameterFile>" 
    printItem workflow 
    printItem File 
done 

使用awk,这将是:

awk ' 
    NR==FNR {workflow[FNR]=$1; next} 
    { 
     print "<ParameterFile>" 
     printf "<workflow>%s</workflow>\n", workflow[FNR] 
     printf "<File>%s</File>\n", $1 
    } 
' file1 file2 

不需要存储第一个文件在内存中的另一种方法:

awk '{ 
    print "<ParameterFile>" 
    print "<workflow>" $0 "</workflow>" 
    getline < "file2" 
    print "<File>" $0 "</File>" 
}' file1 
+0

+1。我从来没有见过“$ {!1}”,所以也很高兴能够了解这一点(我已经评估过了)。尽管任何输入文件中都可能出现空格或反斜杠,但我实际上不会在bash中使用这种方法。 –

1

如果你不介意在一些壳混合:

$ paste -d$'\n' file1 file2 | 
awk '{ printf (NR%2 ? "<ParameterFile>\n<workflow>%s</workflow>\n" : "<File>%s</File>\n"), $0 }' 
<ParameterFile> 
<workflow>SILOS.SIL_Stage_GroupAccountNumberDimension_FinStatementItem</workflow> 
<File>FlatFileConnection.DBConnection_OLAP.SILOS.SIL_Stage_GroupAccountNumberDimension_FinStatementItem.txt</File> 
<ParameterFile> 
<workflow>SDE_ORA11510_Adaptor.SDE_ORA_Stage_GLAccountDimension_FinSubCodes</workflow> 
<File>FlatFileConnection.DBConnection_OLAP.SDE_ORA11510_Adaptor.SDE_ORA_Stage_GLAccountDimension_FinSubCodes.txt</File> 

否则请参阅@ GlennJackman的纯awk方法来解决此问题。

+1

这三种解决方案都很好。谢谢你们的帮助。 – user3232642

相关问题