【发布时间】:2017-11-14 15:23:41
【问题描述】:
我想从单个文件中获取特定文件包含并通过 bash 放入单独的文件中。我尝试使用以下代码获取包含 test1 的文件并且能够获取它,但是在获取受尊重的文件中的所有内容时我失败了。
尝试过的代码:
reportFile=/report.txt
test1File=/test1.txt
test2File=/test2.txt
test3File=/test3.txt
totalLineNo=`cat ${reportFile} | wc -l`
test1LineNo=`grep -n "Test1 file content :" ${reportFile} | grep -Eo '^[^:]+'`
test2LineNo=`grep -n "Test2 file content :" ${reportFile} | grep -Eo '^[^:]+'`
test3LineNo=`grep -n "Test3 file content :" ${reportFile} | grep -Eo '^[^:]+'`
exactTest1LineNo=`echo $(( ${test1LineNo} - 1 ))`
exactTest2LineNo=`echo $(( ${test2LineNo} -1 ))`
exactTest3LineNo=`echo $(( ${test3LineNo} -1 ))`
test1Content=`cat ${reportFile} | head -n ${exactTest1LineNo}`
test3Content=`cat ${reportFile} | tail -n ${exactTest3LineNo}`
echo -e "${test1Content}\r" >> ${test1File}
echo -e "${test3Content}\r" >> ${test3File}
report.txt:
-------------------------------------
My Report:
Test1 file content:
1
2
3
4
5
6
Test2 file content:
7
8
9
10
Test3 file content:
11
12
13
14
15
Note: Find my report above.
-------------------------------------
test1.txt(预期):
1
2
3
4
5
6
test2.txt(预期):
7
8
9
10
test3.txt(预期):
11
12
13
14
15
【问题讨论】:
-
您误用了
cat命令。使用shellcheck 在此处发布之前检查您的代码。