0

I made a small bash script which gets the current air pressure from a website and writes it into a variable. After the air pressure I would like to add the current date and write everything into a text file. The target should be a kind of CSV file.

My problem. I always get a line break between the air pressure and the date. Attempts to remove the line break by sed or tr '\n' have failed.

2nd guess from me: wget is done "too late" and echo is already done.

So I tried it with && between all commands. Same result.

Operating system is Linux. Where is my thinking error?

I can't get any further right now. Thanks in advance.

Sven

PS.: These are my first attempts with sed. This can be written certainly nicer ;)

#!/bin/bash

luftdruck=$(wget 'https://www.fg-wetter.de/aktuelle-messwerte/' -O aktuell.html && cat aktuell.html | grep -A 0 hPa | sed -e 's/<[^>]*>//g' | sed -e '/^-/d' | sed -e '/title/d' | sed -e 's/ hPa//g')

datum=$(date) 

echo -e "${luftdruck} ${datum}"  >> ausgabe.txt
7
  • 1
    Btw.: you can replace aktuell.html && cat aktuell.html with a -. Commented Oct 21, 2022 at 19:19
  • 1
    The use of echo -e is a bug here; you want simply echo, or printf Commented Oct 21, 2022 at 19:24
  • && doesn't have anything to do with this; && means "run the next command only if the first command succeeds". In this case, the && is actually appropriate (it shouldn't try to use the file if it wasn't downloaded successfully), but for reasons that have nothing at all to do with the problem you're trying to solve. Commented Oct 21, 2022 at 19:44
  • Btw: You can combine sed -e ... | sed -e ... into sed -e ... -e ... or sed -e '...; ...'. It is not only shorter, but also way faster (even though it does not really matter in this case). Commented Oct 21, 2022 at 20:31
  • Thank you all for your suggestions for improvement @tripleee - the -e was left over from the trial and error - but had neither positive nor negative effects Commented Oct 21, 2022 at 20:59

2 Answers 2

2

Replace sed -e 's/ hPa//g') with sed -e 's/ hPa//g' | dos2unix) to replace trailing carriage return (DOS/Windows) with line feed (Unix/Linux).

Sign up to request clarification or add additional context in comments.

1 Comment

The nearly perfect answer ;)
1

The html file you download is using Windows line endings (Carriage Return \r + Line Feed \n). I assume your bash script only removes \ns, but the editor you are using to view the file is showing the \r as a linebreak.

Therefore, you could pipe everything through tr -d \\r\\n which would remove all line breaks.

But there is a better alternative: Extract only the important part instead of whole lines.

luftdruck=$(
    wget 'https://www.fg-wetter.de/aktuelle-messwerte/' -O - |
    grep -o '[^>]*hPa' | tr -dc 0-9.
)
echo "$luftdruck $(date)" >> ausgabe.txt

1 Comment

The perfect answer ;) And I realized my thinking problem: wget just downloads data and doesn't interpret anything - because it's not a browser and shouldn't be one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.