Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am not a programmer but I have a web site at https://parliamenthillottawa.com. I have a file that contains formatted text named weather-data-advisory-temp.txt and I want to find the word "Advisory" and save the line that it is in and the next 2 number of lines.
(BUTTON) Main Menu
* [6]Jobs and the workplace
* [7]Immigration and citizenship
* [8]Travel and tourism
* [9]Business and industry
* [10]Benefits
* [11]Health
* [12]Taxes
* [13]Environment and natural resources
* [14]National security and defence
* [15]Culture, history and sport
* [16]Policing, justice and emergencies
* [17]Transport and infrastructure
* [18]Canada and the world
* [19]Money and finances
* [20]Science and innovation
You are here:
1. [21]Home
2. [22]Environment and natural resources
3. [23]Weather information
4. [24]Alerts
[25]Edit My ProfileMy Weather Profile
[26]Weather shortcuts
Access city
____________________
(BUTTON) Go
(BUTTON) Last updated5:31 AM local Tue 28 Nov 2023
Alerts for: Ottawa North - Kanata - Orléans
Statements
11:50 PM EST Monday 27 November 2023
Weather Advisory ended for:
* City of Ottawa
Follow:
* [27]'Regional ATOM feed' | translate Regional ATOM
Weather shortcuts
(BUTTON) Add to shortcuts (BUTTON) Organize shortcuts
Organize Shortcuts
Select to drag and drop, rename or delete.
No shortcuts
* [28]Report Severe Weather
* [29]Canadian Weather
* [30]Weather Radar - Canada
* [31]Satellite - Canada
* [32]Marine - Canada
* [33]Air Quality
* [34]Canadian Ice Service
* [35]Seasonal forecasts
* [36]Extended forecast
* [37]Public Alerts - Canada
* [38]Lightning
* [39]Hurricane
* [40]Weather Summaries
* [41]Hazardous Weather
* [42]Global and Regional forecast models
* [43]Astronomy
* [44]Water Level and Flow
* [45]Historical Climate Data
* [46]Aviation
* [47]Guide to Public forecasts
* [48]Guide to Marine forecasts
* [49]Guide to Air Quality forecasts
* [50]Canadian Centre for Climate Services Library
* [51]Wind Chill and Humidex Calculators
* [52]Weather and your health
Add this page
Name ____________________
(BUTTON) Save (BUTTON) Cancel
(BUTTON) Rename
(BUTTON) Delete
(BUTTON) Save
×
Add to shortcuts
The name you have entered for the shortcut already exists on your
Weather shortcuts menu. Would you like to overwrite it?
(BUTTON) Yes (BUTTON) No
×
Rename shortcut
There is already a shortcut with the same name in this list. Do you
want to rename "link" to "link(2)"?
(BUTTON) Yes (BUTTON) No
×
Add to shortcuts
Your shortcut list has reached the maximum size of 30
(BUTTON) Close
How to use
* Add to shortcuts
+ Go to the desired page on the site, open the Weather shortcuts
menu and click on the "Add to shortcuts" button
+ Page name will display in the "Add this page" window, the name
can be changed by highlighting the text and entering the
desired name
+ Click on the "Save" button to add the link to the list
+ Click on the "X" button to close the panel
* Delete a link
+ Click on the "Organize shortcuts" button
+ Select the link to delete
+ Click on the "Delete" button to remove the link from the list
+ Click on the "Close" button to save the change
* Move a link up/down the list
+ Click on the "Organize shortcuts" button
+ Click and hold the link and move to new location in the list
+ Click on the "Close" button to save the change
* Rename a link
+ Click on the "Organize shortcuts" button
+ Select the link to rename
+ Click on the "Rename" button and change the link name
+ Click the enter or tab key
+ Click on the "Close" button to save the change
* Keyboard navigation
+ Use the Tab key to move to a button or link
+ Use the Enter key to click on buttons or links
+ Organize shortcuts navigation
o Use the ↑ Up key to select the link above. If none are
selected, it will select the first link.
o Use the ↓ Down key to select the link below. If none are
selected, it will select the last link.
o Use the Page Up key to move a selection up in the list.
o Use the Page Down key to move a selection down in the
list.
* Restore default list
Warning: Clicking on the button below will remove all your
customized links.
(BUTTON) Restore default links
Customized Weather shortcuts
Save your customized list as a bookmark.
Copy/paste or click on the link below then when the page displays, you
can bookmark the page using the web browser. Bookmarking your
customized list will allow you to access it even if the local storage
on your device is erased.
(BUTTON) Close
[53]Feedbackabout this web site
Date modified:
2023-10-24
About this site
Environment and natural resources
* [54]Contact us
* [55]Our environment
* [56]Nature-based climate solutions
Government of Canada
* [57]All contacts
* [58]Departments and agencies
* [59]About government
Themes and topics
* [60]Jobs
* [61]Immigration and citizenship
* [62]Travel and tourism
* [63]Business
* [64]Benefits
* [65]Health
* [66]Taxes
* [67]Environment and natural resources
* [68]National security and defence
* [69]Culture, history and sport
* [70]Policing, justice and emergencies
* [71]Transport and infrastructure
* [72]Canada and the world
* [73]Money and finance
* [74]Science and innovation
* [75]Indigenous peoples
* [76]Veterans and military
* [77]Youth
Government of Canada Corporate
* [78]Social media
* [79]Mobile applications
* [80]About Canada.ca
* [81]Terms and conditions
* [82]Privacy
Check the various options in the manual page, "man grep". The -h or --no-filename option might be what you are looking for. Maybe -m or --max-count, too.
In general, you are better off trying to extract your data using a proper HTML parser using Perl or Python. There are even standalone tools like xmllint or xmlstarlet.
grep seems to be grabbing the "*" in the file? Maybe use Python:
Code:
> python3
Python 3.9.18 (main, Sep 15 2023, 12:58:45)
[GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open( "weather-data-advisory-temp.txt", "r" ) as infile:
... for line in infile:
... if "Advisory" in line:
... line2 = infile.readline()
... line3 = infile.readline()
... print( line, end="" )
... print( line2, end="" )
... print( line3, end="" )
...
Weather Advisory ended for:
* City of Ottawa
>>>
Add 'set -f' to your script before the offending line(s) to disable globbing. Note: -f disables globbing completely, so adding 'set +f' after to re-enable it is a very good idea. Or you may enclose the variable in quotes but not like suggested in the previous message - that won't work, but the other way around: echo "$alert"
And BTW by the offending line I mean the line where the variable is expanded. Everyone in this thread makes the same mistake by assuming that unwanted globbing occurs in the grep line, but it isn't so.
Add 'set -f' to your script before the offending line(s) to disable globbing. Note: -f disables globbing completely, so adding 'set +f' after to re-enable it is a very good idea. Or you may enclose the variable in quotes but not like suggested in the previous message - that won't work, but the other way around: echo "$alert"
And BTW by the offending line I mean the line where the variable is expanded. Everyone in this thread makes the same mistake by assuming that unwanted globbing occurs in the grep line, but it isn't so.
You are right, it is the echo line where this globbing occurs, not the grep line.
Quote:
Originally Posted by gilesaj001
I used the pan64 idea which is what I had except I had the quotes in the wrong place.
Now I want to add the advisory to a php page to display on my site.
I tried the following but just the code was displayed.
# Normally NOT needed for a var=value assignment:
alert=$(grep --after-context=2 'Advisory' weather-data-advisory-temp.txt)
# But needed in commands, like the echo command:
echo "$alert"
echo "Matches:
$(grep --after-context=2 'Advisory' weather-data-advisory-temp.txt)"
The quotes even protect the newline in between i.e. preserve the two lines.
Last edited by MadeInGermany; 11-28-2023 at 03:48 AM.
# Normally NOT needed for a var=value assignment:
alert=$(grep --after-context=2 'Advisory' weather-data-advisory-temp.txt)
# But needed in commands, like the echo command:
echo "$alert"
The quotes even protect the newline in between i.e. preserve the two lines.
On the command line this works great but in my php file it just shows the line "alert=$(grep --after-context=2 'Advisory' weather-data-advisory-temp.txt)" as text on the page.
(Since the pattern stops at empty lines, I used 6 instead of 2 for the upper-bounds; that number can/should be adjusted as appropriate.)
I put this in my php file:
Code:
>?php
$filedata = file_get_contents("I did put in the path to weather-data-advistory-temp.txt");
preg_match_all( "/Weather Advisory.*(?:\n[^\r\n]+){0,6}/" , $filedata , $alert );
echo $alert[0][0];
?>
Yes you are correct and I no longer get the text on the web page. I will have to wait till there is another alert to see if it works.
@boughtonp -- It looks like if there is an alert the text is as shown BUT if there is no alert the following text is on the file.
Quote:
Alerts for: Ottawa North - Kanata - Orléans
Ottawa North - Kanata - Orléans
2:05 AM EST Wednesday 29 November 2023
No alerts in effect.
What would be the command to find this be as I cannot follow what you did, old timers I think.
I would like to look for the first text as you have given, "Weather Advisory" If not found then search for the second text "Alerts for: Ottawa North - Kanata - Orléans"
Last edited by gilesaj001; 11-29-2023 at 01:22 AM.
I have a script that dumps the results of a query into a txt file.
Code:
#!/usr/bin
#!/bin/bash
#!/usr/sbin
cd path to script
alert="$(grep 'Warnings' --after-context=7 weather-data-advisory.txt)"
echo "$alert" > find-warnings.txt
This outputs this text at the moment as there is a warning
Quote:
Warnings
12:26 AM EST Sunday 3 December 2023
Snowfall Warning in effect for:
* City of Ottawa
Heavy snowfall expected today into Monday morning. Hazards: Snowfall
accumulations of 15 to 20 cm. Reduced visibility at times in heavy
Then on my php page I have this
Code:
<?php
$message=0;
$filename = "/path to txt file/find-warnings.txt";
$message = file_get_contents($filename);
if (empty($message))
{
echo "No Alerts in Effect";
}
else
{
echo "$message";
}
?>
Now as I said this is working but it outputs the text in one long row on the web page.
Quote:
Warnings 12:26 AM EST Sunday 3 December 2023 Snowfall Warning in effect for: * City of Ottawa Heavy snowfall expected today into Monday morning. Hazards: Snowfall accumulations of 15 to 20 cm. Reduced visibility at times in heavy
Is there a way to get the text in the same format as the text file ? The format might change depending on the warning ?
Last edited by gilesaj001; 12-03-2023 at 12:51 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.