BASH脚本:分解字符串并保存到文件
我需要一些帮助。我有这个 info.txtBASH脚本:分解字符串并保存到文件,bash,shell,ubuntu,awk,command-line-interface,Bash,Shell,Ubuntu,Awk,Command Line Interface,我需要一些帮助。我有这个 info.txt [test.local] user=test group=test ; [game.local] user=game group=game ; [test.local] user=test group=test [game.local] user=game group=game 这是我的目标,我希望它与分开并将其放入文件中,文件名基于[] 像这样 test.local.txt [test.local] user=test group=test ;
[test.local]
user=test
group=test
;
[game.local]
user=game
group=game
;
[test.local]
user=test
group=test
[game.local]
user=game
group=game
这是我的目标,我希望它与分开并将其放入文件中,文件名基于[]
像这样
test.local.txt
[test.local]
user=test
group=test
;
[game.local]
user=game
group=game
;
[test.local]
user=test
group=test
[game.local]
user=game
group=game
game.local.txt
[test.local]
user=test
group=test
;
[game.local]
user=game
group=game
;
[test.local]
user=test
group=test
[game.local]
user=game
group=game
这里是我当前的代码files.sh
#!/bin/bash
value=$(<info.txt)
SAVEIFS=$IFS
IFS=$';'
val=($value)
IFS=$SAVEIFS
for (( i=0; i<${#val[@]}; i++ ))
do
echo "${val[$i]}"
done
#/bin/bash
value=$(您可以使用以下命令:
awk -v RS=';\n' 'NF{f=$1; gsub(/[][]/, "", f); printf "%s", $0 > (f ".txt")}' info.txt
详细信息:
-v RS=';\n'
:将输入记录分隔符设置为;
,后跟换行符
NF{…}
:仅对非空行执行
f=$1
:保存变量f
gsub(/[]]/,“”,f)
:从变量f
printf
:将单个块重定向到由f
值和文本“.txt”
您可以使用
fn=$(echo "${val[$i]}"|head -n 1| tr -d '[]').txt
查找要创建的文件的名称并
echo "${val[$i]}"|tail -n +2
生成该文件的内容。您的具体问题是什么?您编写了哪些代码来实现此目的?@user1934428嗨,先生,已经更新。谢谢先生。这是有效的。您能告诉我有关此命令及其选项的详细信息吗?这意味着什么。对不起,我的意思是我在回答中添加了解释。先生。我如何添加您可以添加文件上的路径。只需在重定向>hi sir后将其添加到f之前。更改[test.local]和[game.local]将不会包含在txt文件中,其余内容相同。