Yesterday, I’ve received the following error:
head: illegal line count -- -4 as I was trying to remove the last 4 lines from a sitemap.xml file. This is called negative arguments. It was weird since it only happened on a macOS machine.
Step 1. I found out that the native version of
head doesn’t support negative line counts. Instead, install GNU’s
ghead version which is part of the
coreutils Homebrew package.
brew install coreutils
Step 2. Replace
ghead. For instance:
curl https://devcoops.com/sitemap.xml | sed '/^<loc>/!d' | sed -e 's/<[^>]*>//g' | ghead -n -4 > temp
Using regular expressions for parsing XML is strongly discouraged since it’s a hard thing to do in practice. There are better ways and tools for sure.
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.