Search code examples
bashshellunixsedxmlstarlet

XMLStarlet remove last -n (newline) using Sed


This is a rather more simpler task which has been troubling me for some time. I have the following:

homepage=$(curl "https://example.com/")

xmlstarlet --quiet fo --html <<<"$homepage" |
xmlstarlet sel -T -t \
    -m "//*[@id='financial']/tbody/tr/td" \
        --if 'not(starts-with(a//@href,"http"))' \
          -o 'https://example.com' \
        --break \
        -v 'a//@href' \
        -o '/?start=1' \
        -o '&' \
        -o 'end=2' -n | \ 
            sed '${/^$/d;}' \ 
                >> "results.txt"

What I want to do is remove the last newline produced by xmlstarlet in -o 'end=2' -n | \. When it reaches the end if the link list it still produces a -n (newline) as if it where to continue adding more links, but actually I want to avoid the last -n instance respective to the last href.

My sed '${/^$/d;}' \ that should do this returns the following error:

sed: ${/^$/d;}: No such file or directory
sed:  : No such file or directory

It somehow does not pipe the previous STDOUT to the sed STDIN correctly. In one of my prior questions I worked with something similar and this sed command worked for me earlier:

sed 's/\\&amp;/\&/g'

On the other hand, I also tried using:

# The -e flag
sed -e '${/^$/d;}'

Which did not work for me either.

Can this be done directly from XMLStarlet without having to add an extra sed pipe?

What is wrong with my sed? What is the correct sed method?


Solution

  • Placing this at the end of the script worked for me:

    printf "%s" "$(</results.txt)" > results.txt
    

    I was looking for doing this directly in XMLStarlet, hence this is a provisional answer.