[英]How to save automatically with append comment to file name?
I am extracting data from files and I'd like to apply these working (ugly) command lines to all the txt files from a given folder. 我正在从文件中提取数据,我想将这些工作的(丑陋的)命令行应用于给定文件夹中的所有txt文件。 Thus I would also need to append a string to the output file name to avoid overwriting during the loop... any suggestion is warmly welcome.
因此,我还需要在输出文件名后附加一个字符串,以避免在循环过程中被覆盖...热烈欢迎任何建议。
for file in ./TEST/;
do
awk '/R.Time/,/LC/' 070_WT3a.txt|awk '/R.Time/,/PDA/'|grep -v -E "PDA|LC"|grep -w -v "80,00000"|grep -w -v "80,00833"|grep -w -v "80,01667"|grep -w -v "80,01067"|grep -w -v "80,02133"|sed -n '1,9601p' > ./Output/Fluo.txt;
awk '/R.Time/,/LC/' 070_WT3a.txt|awk '/R.Time/,/PDA/'|grep -v -E "PDA|LC"|grep -w -v "80,00000"|grep -w -v "80,00833"|grep -w -v "80,01667"|grep -w -v "80,01067"|grep -w -v "80,02133"|sed -n '9603,19203p' > ./Output/RID.txt;
done
Inside the loop you can use the variable ${file}
. 在循环内,您可以使用变量
${file}
。 A first improvement (with additional lines that you can add after a pipe) : 第一个改进(可以在管道之后添加其他行):
for file in ./TEST/;
do
filebasename=${file##*/}
awk '/R.Time/,/LC/' ${file}.txt |
awk '/R.Time/,/PDA/' |
grep -v -E "PDA|LC" |
grep -w -v "80,00000"|
grep -w -v "80,00833"|
grep -w -v "80,01667"|
grep -w -v "80,01067"|
grep -w -v "80,02133"|
sed -n '1,9601p' > ./Output/Fluo_${filebasename};
awk '/R.Time/,/LC/' 070_WT3a.txt |
awk '/R.Time/,/PDA/'|
grep -v -E "PDA|LC"|
grep -w -v "80,00000"|
grep -w -v "80,00833"|
grep -w -v "80,01667"|
grep -w -v "80,01067"|
grep -w -v "80,02133"|
sed -n '9603,19203p' > ./Output/RID_${filebasename};
done
The next thing you can do is improving the parsing. 您可以做的下一件事是改进解析。
Without example input/output it is hard to see/test a solution, I can not tell for sure that all files needs to be split on lines 9601/9603/19203, what seems to be working for 070_WT3a.txt. 没有示例输入/输出,很难看到/测试解决方案,我无法确定所有文件都必须在行9601/9603/19203上拆分,这似乎适用于070_WT3a.txt。 I would like to start with skipping the
80*
lines, but these lines might have the boundaries R.Time/LC inside, so that won't help. 我想先跳过
80*
行,但是这些行可能在内部带有R.Time / LC边界,所以这无济于事。 You might want to test on 070_WT3a.txt
with 您可能想使用以下命令在
070_WT3a.txt
上进行测试
awk '/R.Time/,/LC/' 070_WT3a.txt |awk '/R.Time/,/PDA/'|
grep -v -E "PDA|LC"|grep -Ewv "80,0(0000|0833|1667|1067|2133)"
You can try to combine the 2 awk
's into one (or even get the grep
's inside the awk
, but that is becoming offtopic and difficult to test without clear requirements and examples. 您可以尝试将两个
awk
组合成一个(甚至将grep
放入awk
,但是这变得不合时宜,如果没有明确的要求和示例,很难进行测试。
EDIT: After testing with an example input I found this simplified: 编辑:使用示例输入进行测试后,我发现它得到了简化:
for file in TEST/*.txt; do
filebasename=${file##*/}
awk '/LC Chromatogram(Detector A-Ch1)/,/^80,/' "${file}" |
grep -E "^[0-7]" > Output/Fluo_${filebasename}
awk '/LC Chromatogram(Detector B-Ch1)/,/^80,/' "${file}" |
grep -E "^[0-7]" > Output/RID_${filebasename}
done
Inside the loop I use ${file}, that will have different filenames each loop. 在循环中,我使用$ {file},每个循环将具有不同的文件名。 The filenaam is also used for the name of the outputfiles.
filenaam也用于输出文件的名称。 The filename will start with
TEST/
, that can be stripped with ${file##*/}
(there are a lot different ways like using cut -d"/"
and sed 's/..
, this one is fast). 文件名将以
TEST/
开头,可以用${file##*/}
剥离(有很多不同的方式,例如,使用cut -d"/"
和sed 's/..
,这是很快速的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.