[英]JSON file with input from filename using bash
I am very new to bash scripting. 我对bash脚本非常陌生。
I have set of JSON files as follows: 我有一组JSON文件,如下所示:
sub-285345_task-EMOTION_acq-LR_idir-acq-LR_epi.json
sub-285345_task-EMOTION_acq-LR_idir-acq-RL_epi.json
sub-285345_task-EMOTION_acq-RL_idir-acq-LR_epi.json
sub-285345_task-EMOTION_acq-RL_idir-acq-RL_epi.json
Now looking at the each .json filename, I want to have key value pair inside the .json file. 现在查看每个.json文件名,我想在.json文件中包含键值对。 For eg: for file sub-285345_task-EMOTION_acq-LR_idir-acq-LR_epi.json
i want to include following information: 例如:对于文件sub-285345_task-EMOTION_acq-LR_idir-acq-LR_epi.json
我要包含以下信息:
{
"PhaseEncodingDirection": "-i",
"TotalReadoutTime": 0.08346,
"IntendedFor": "func/sub-285345_task-rest_acq-LR_run-01_bold.nii.gz"
}
PhaseENcodingDirection
is derived from idir-acq-LR
in the filename. PhaseENcodingDirection
是从文件名中的idir-acq-LR
派生的。 For LR
its -i
and for RL
its i
. 对于LR
其-i
和RL
其i
。
How can this be done using bash script preferably if not then in python. 最好在不使用python的情况下如何使用bash脚本完成此操作。
Any help is appreciated. 任何帮助表示赞赏。
You try below node js script 您尝试下面的节点js脚本
var fs = require('fs');
fs.readdir('.', function(err, files) {
if (err) console.log(err);
files.forEach(function(file) {
var obj = {
"TotalReadoutTime": 0.08346,
};
if (file.indexOf("idir-acq-LR") > -1) {
console.log(file);
obj.PhaseEncodingDirection = "-i";
var intendedForPart1 = file.replace(/'-EMOTION-acq-LR'/g,'-rest_acq-LR');
var intendedForPart2 = intendedForPart1.replace(/'_epi.json'/g,'_run-01_bold.nii.gz');
obj.IntendedFor = 'func/' + intendedForPart2;
fs.writeFile(file, JSON.stringify(obj), 'utf-8');
} else if (file.indexOf("idir-acq-RL") > -1) {
console.log(file);
obj.PhaseEncodingDirection = "i";
var intendedForPart1 = file.replace(/'-EMOTION-acq-RL'/g,'-rest_acq-RL');
var intendedForPart2 = intendedForPart1.replace(/'_epi.json'/g,'_run-01_bold.nii.gz');
obj.IntendedFor = 'func/' + intendedForPart2;
//obj.IntendedFor = "func/sub-285345_task-rest_acq-RL_run-01_bold.nii.gz"
fs.writeFile(file, JSON.stringify(obj), 'utf-8');
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.