Skip to content

Instantly share code, notes, and snippets.

@mondeja
Created September 23, 2022 22:22
Show Gist options
  • Save mondeja/b515dbe06c3a92d9da05d8fac5fbd6dd to your computer and use it in GitHub Desktop.
Save mondeja/b515dbe06c3a92d9da05d8fac5fbd6dd to your computer and use it in GitHub Desktop.
po2txt
#!/bin/sh
output=""
inside_msgstr=0
newline=""
add_newline_to_output() {
if [ -z "$output" ]; then
output="${newline}\n"
else
output="${output}${newline}\n"
fi;
}
while read line; do
if [ -z "$line" ]; then
if [ "$inside_msgstr" -eq "1" ]; then
add_newline_to_output
inside_msgstr=0
newline=""
fi;
continue
fi;
command="$(printf '%s' "$line" | cut -c -8)"
first_char="$(echo "$line" | cut -c 1)"
msgstr=""
if [ "$command" = 'msgstr "' ]; then
inside_msgstr=1
msgstr="$(printf '%s' "$line" | cut -c 9-)"
elif [ "$inside_msgstr" -eq 1 ] && [ "$first_char" = '"' ]; then
msgstr="$(printf '%s' "$line" | cut -c 2-)"
else
inside_msgstr=0
newline=""
continue
fi;
newline="$newline$(printf '%s' "$msgstr" | rev | cut -c 2- | rev)"
if [ -z "$newline" ]; then
continue
fi;
done < $1
add_newline_to_output
echo "$output" > $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment