I guess what you need is sed and perhaps awk. But I'm still not familiar with these power tools. You can modify stings with sed using its \s command. You load the source file into an array of strings and then modify each string in a cycle, writing it in the output file in whichever format you want.
I have a such script. I stumbled upon it somewhere on the internet and then spoiled it to serve my purposes. This script takes GTK bookmarks file and then translates it into an Openbox pipemenu, where I can open bookmarks via file manager. That's the script:
#!/bin/bash
echo '<openbox_pipe_menu>'
filemanager="pcmanfm"
bookmarksfile="/home/$USER/.config/gtk-3.0/bookmarks"
thepaths=(`sed 's/[ ][^ ]*$//' ${bookmarksfile}`)
thenames=(`sed 's/^[a-zA-Z0-9а-яА-Я_%:\/]* //' ${bookmarksfile}`)
for i in ${!thepaths[@]} ; do
echo '<item label="'${thenames[$i]}'">'
echo '<action name="Execute"><execute>'
echo "$filemanager ${thepaths[$i]}"
echo '</execute></action>'
echo '</item>'
done
echo '</openbox_pipe_menu>'
Don't ask me how exactly it works. I spent quite a while trying to understand how sed exactly works. All I can say, it builds the two arrays of strings: $thepaths is file paths, $thenames is obviously the names of the bookmarks. The bookmarks file contains paths and names on separate strings. The paths comes first, then comes a space, then comes the name. Here is the input:
file:///home/victor/Documents Documents
file:///home/victor/Universitas Universitas
file:///home/victor/Documents/books books
file:///home/victor/Documents/Arbeit Arbeit
file:///home/victor/Documents/Arbeit/%D0%BF%D0%B5%D1%80%D0%B5%D0%B2%D0%BE%D0%B4%D1%8B переводы
file:///home/victor/Pictures Pictures
file:///home/victor/Downloads Downloads
Here's the output:
<openbox_pipe_menu>
<item label="Documents">
<action name="Execute"><execute>
pcmanfm file:///home/victor/Documents
</execute></action>
</item>
<item label="Universitas">
<action name="Execute"><execute>
pcmanfm file:///home/victor/Universitas
</execute></action>
</item>
<item label="books">
<action name="Execute"><execute>
pcmanfm file:///home/victor/Documents/books
</execute></action>
</item>
<item label="Arbeit">
<action name="Execute"><execute>
pcmanfm file:///home/victor/Documents/Arbeit
</execute></action>
</item>
<item label="переводы">
<action name="Execute"><execute>
pcmanfm file:///home/victor/Documents/Arbeit/%D0%BF%D0%B5%D1%80%D0%B5%D0%B2%D0%BE%D0%B4%D1%8B
</execute></action>
</item>
<item label="Pictures">
<action name="Execute"><execute>
pcmanfm file:///home/victor/Pictures
</execute></action>
</item>
<item label="Downloads">
<action name="Execute"><execute>
pcmanfm file:///home/victor/Downloads
</execute></action>
</item>
</openbox_pipe_menu>