答案1
这是一种方法:
## save the first line of one.txt in the variable $string
string=$(head -n1 one.txt)
## delete the first line of one.txt
sed -i '1d' one.txt
## replace the Xs in `>>XXXXX<<` with the contents of `$string`
## and save as the new file "$string.txt" (AAAA.txt)
sed "s/>>XXXXXXX<</>>$string<</" sample.txt > $string.txt
>>XXXXXX<<
请注意,这假设的任何行上仅出现一次sample.txt
。如果每行可以有多个,则上面的命令将仅替换每行上的第一个。要替换所有这些,请使用以下命令:
sed "s/>>XXXXXXX<</>>$string<</g" sample.txt > $string.txt
您原来的问题在每行末尾都有空格one.txt
。如果您的真实文件就是这种情况,并且您需要在添加到 之前删除空格sample.txt
,请使用以下命令:
string=$(head -n1 one.txt | sed 's/ *$//')
然后与上面相同的命令。