[英]Bash Copy multiple lines in seperate files
i have one file with follow entries, is very long so show few examples: 我有一个带有以下条目的文件,非常长,因此请显示几个示例:
media-libs/gd fontconfig xpm
media-sound/audacious -gtk gtk3
net-analyzer/wireshark -gtk3 -ssl -qt4 qt5 lua geoip kerberos adns
net-dialup/ppp -gtk
I need to copy every line in a new file called like the name between / and first spaces. 我需要在新文件中复制每一行,例如/和第一个空格之间的名称。
So that i have a file called gd, audacious, wireshark and ppp with appropriate content behind name. 这样我就有了一个名为gd,audacious,wireshark和ppp的文件,其名称后面带有适当的内容。
media-libs/gd fontconfig xpm
media-sound/audacious -gtk gtk3
net-analyzer/wireshark -gtk3 -ssl -qt4 qt5 lua geoip kerberos adns
net-dialup/ppp -gtk
Thank you for help & wish nice weekend 感谢您的帮助并祝您周末愉快
Silvio 西尔维奥
I think you need something like this, but please try it in a separate directory where you just have a COPY of your data: 我认为您需要类似的内容,但是请在单独的目录中尝试一下,在该目录中您只需复制数据即可:
awk '{f=$1;sub(/.*\//,"",f);print > f}' yourFile
It may not be that robust, but it takes the first field of each line, strips everything up to a slash and saves that as f
, then writes the whole line to file f
. 它可能不那么健壮,但是它占用了每一行的第一个字段,将所有内容剥离到一个斜杠并将其保存为
f
,然后将整行写入文件f
。
When you want to loop with a while loop, you can look at: 如果要使用while循环进行循环,可以查看:
while read -r line; do
echo "line=${line}"
noSlash=${line##*/}
echo "noSlash=${noSlash}"
filename=${noSlash%% *}
echo "filename=${filename}"
content=${noSlash#* }
echo "Content=${content}"
done < input
The input file at the last line of the code is parsed 1 line each time. 每次都将代码最后一行的输入文件解析为1行。 The variables are parsed using
#
and %
. 使用
#
和%
解析变量。 The result is echoed in the above code for testing. 结果在上面的代码中回显以进行测试。 WHen you are happy with the solution, you can use
如果您对解决方案感到满意,可以使用
while read -r line; do
noSlash=${line##*/}
filename=${noSlash%% *}
content=${noSlash#* }
printf "%s\n" "${content}" > "${filename}"
done < input
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.