XML에서 쉘 스크립트로 데이터를 하나씩 구문 분석해야 함
Requriment :- we need to take <FileSetfolder> as one record. XML may contain many <FileSetFolder> from Each <FileSetFolder> record.
we need to extract <path> and <Filetypes> and archive all the files which are with extension type as <Filetypes>
need help in approach for this requirement
ex:- for first <FileSetfolder>
Archive_path = D:\apache-2.4.10\htdocs
Filetypes =(rep,zip,mnt)
need to archive files in $Archive_path which have $Filetypes (rep,zip,mnt)
then it should take next <FileSetfolder>
XML:-
<SourceFolder>
<FileSetFolder>
<Path>D:\apache-2.4.10\htdocs</Path>
<FileType>rep</FileType>
<FileType>zip</FileType>
<FileType>mnt</FileType>
</FileSetFolder>
<FileSetFolder>
<Path>D:\Download\ROSXcenterAutoArchive\ArchiveStorage\archive</Path>
<FileType>mnt</FileType>
<FileType>952230</FileType>
</FileSetFolder>
</SourceFolder>
답변1
사용XML 스타(때때로 대신 설치됨 xmlstarlet
) xml
:
paths=( $( xml sel -t -v '//FileSetFolder/Path' file.xml ) )
for path in "${paths[@]}"; do
filetypes=( $( xml sel -t -v "//FileSetFolder[Path=\"$path\"]/FileType" file.xml ) )
for filetype in "${filetypes[@]}"; do
printf 'Path "%s" has a filetype "%s"\n' "$path" "$filetype"
done
done
산출:
Path "D:\apache-2.4.10\htdocs" has a filetype "rep"
Path "D:\apache-2.4.10\htdocs" has a filetype "zip"
Path "D:\apache-2.4.10\htdocs" has a filetype "mnt"
Path "D:\Download\ROSXcenterAutoArchive\ArchiveStorage\archive" has a filetype "mnt"
Path "D:\Download\ROSXcenterAutoArchive\ArchiveStorage\archive" has a filetype "952230"