Using shell to replace characters in a text file

Can I just say, this is such a frustrating and yet enormously rewarding field of study. I'm in the middle of configuring GeekTool (Uh oh, stupid n00b) and I really only have one question.

I'm using Automator to grab a RSS feed, having GeekTool continually run that application every 10 minutes, putting the results in a text file and then displaying that .txt file. However, the RSS codes their dates by their standards and I don't know how to intercept that and change them to my own time zone.

Here's a picture:

Here's the GeekTool code that runs it:

head -n 300 ~/Documents/Notes/GeekTool/RSS/Cheatsheet.txt | tr '[\n]' '~' | sed 's/~~/\
/g' | sed 's/~//g' | sed 's/-0000//g'

----

Bonus question: Why is my script cutting off the text file at "NASA unveils ..."? There's plenty room in the window and the actual text file has more content than that. Is there a character limit cut-off that I'm missing in the code?

Thanks for reading!

Without seeing the data which produced that, we're only guessing.

Oh okay. What other information do you need? The automator script?

The code I copied and pasted above is what I put in the "Command" line of GeekTool. There isn't a separate Shell file beyond that.

Give us the original input and desired output

--ahamed

Okay. So basically I'm extracting from a xml feed and converting it to plain text on a locally stored file.

So the .xml is saved and converted to a plain text file

Now I use GeekTool and a Shell script to display that file on my desktop

head -n 300 ~/Documents/Notes/GeekTool/RSS/Cheatsheet.txt | tr '[\n]' '~' | sed 's/~~/\
/g' | sed 's/~//g' | sed 's/-0000//g'

----

Now my question is how do I take these dates that were pulled from the xml file:

and alter them using a Shell script to display my desired time?

My desired output would be the date converted to my time zone: Eastern Time Zone, (UTC05). So basically the same format "yyyy-mm-dd hh:mm:ss" (remove the +0000) but just converted to my time. Can shell script do that?

Something like this?

#!/bin/bash
while read line
do
  from_date=$( echo $line | sed -n 's/.* - \([0-9]*-.*\) +0000/\1/gp' )
  if [ ! -z "$from_date" ]
  then
    to_date=$( TZ=UTC date '+%F %k:%M:%S' -d "$from_date IST" )
    line=$( echo $line | sed "s/$from_date +0000/$to_date/g" )
  fi
  echo $line
done < inputFile


root@bt:/tmp# cat inputFile 
NEW DOLPHIN SPECIES DISCOVERED - 2011-09-16 13:09:00 +0000
IN AUSTRALIAN BAY
root@bt:/tmp# 
root@bt:/tmp# ./run
NEW DOLPHIN SPECIES DISCOVERED - 2011-09-16 7:39:00
IN AUSTRALIAN BAY
root@bt:/tmp# 

And please try to paste the code in test format!

--ahamed

Ahamed, I hope you're patient with me. I'm still learning so much. So I pasted in your code with my previous code to make this:

head -n 300 ~/Documents/Notes/GeekTool/RSS/Cheatsheet.txt | tr '[\n]' '~' | sed 's/~~/\
/g' sed 's/~//g' | sed 's/-0000//g'

#!/bin/bash
while read line
do
  from_date=$( echo $line | sed -n 's/.* - \([0-9]*-.*\) +0000/\1/gp' )
  if [ ! -z "$from_date" ]
  then
    to_date=$( TZ=UTC date '+%F %k:%M:%S' -d "$from_date IST" )
    line=$( echo $line | sed "s/$from_date +0000/$to_date/g" )
  fi
  echo $line
done < inputFile

root@bt:/tmp# cat inputFile 
NEW DOLPHIN SPECIES DISCOVERED - 2011-09-16 13:09:00 +0000
IN AUSTRALIAN BAY
root@bt:/tmp# 
root@bt:/tmp# ./run
NEW DOLPHIN SPECIES DISCOVERED - 2011-09-16 7:39:00
IN AUSTRALIAN BAY
root@bt:/tmp#

Now nothing displays. Maybe that's a GeekTool thing. In fact, maybe I should put your code in a separate .sh file and run that file instead of pasting both segments of the full code right into GeekTool like how I'm currently doing it. What do you think?

You haven't told us how you're converting this XML to text, so it's STILL difficult to really get a grip on your input data. But I see you've shown what the URL for the XML data actually is, which lets me do this:

$ cat geektool.sh
#!/bin/bash

wget -q http://www.tdbimg.com/ext/rss/cheatsheet/rss_cheatsheet.xml -O - |
# Convert xml into text.  Set TZ to our time offset.
        awk -v TZ="$(date +%:z)" -v RS="<entry>" -v FS="\n" 'BEGIN {
        # Convert [+-]HH:MM into an integer value in seconds
        split(TZ, A, ":");
        if(A[1] < 0)    OFF=((A[1]*60) - A[2])*60;
        else            OFF=((A[1]*60) + A[2])*60;
}

NR>1{   # Fix those pesky embedded tags
        gsub("<", "<", $0);  gsub(">", ">", $0);
        # Loop through each line, looking for the ones we want
        for(N=1; N<=NF; N++)
        {
                if($N ~ /<(title|summary)/)
                {       # Strip out tags
                        gsub(/ *<[^>]+>/, "", $N);
                        print $N
                }
                else if($N ~/<updated/)
                {       # Strip out tags
                        gsub(/<[^>]+>/, "", $N);
                        # Convert from YYYY-MM-DD to YYYY MM DD etc.
                        gsub(/[-T:Z]/, " ", $N);
                        # Convert from UTC to local time
                        print strftime("%F %T", mktime($N)+OFF);
                }
        }

        printf("\n");
}'

$ ./geektool.sh
Quake Hits Off Japan Coast
2011-09-16 14:30:00
No tsunami warning issued.

Microsoft to Kill Windows XP
2011-09-16 14:25:00
Will not extend support beyond April 2014.

Pitt Felt 'Pathetic' While Married to Aniston
2011-09-16 13:17:00
Dishes on relationship to 'Parade' magazine.

29 Protesters Killed in Syria
2011-09-16 11:47:00
Most violent day in weeks.

Clinton: Women Can Rescue the Economy
2011-09-16 11:04:00
Delivers declaration at APEC summit in San Francisco.

Fall TV Is a Step Back for Women
2011-09-16 10:56:00
Despite more female producers, directors, actors.

Abbas to Seek Palestinian State
2011-09-16 10:21:00
At U.N. Security Council.

Police Pressure Guardian to Reveal Sources
2011-09-16 10:17:00
Threaten paper with espionage law.

Two Men Use Dead Friend�s ATM
2011-09-16 08:40:00
Drive around with corpse, use his cash at strip club.

Congress's Ratings Hit Record Low
2011-09-16 08:30:00
Just 12 percent of Americans approve.

Rogue Trader Charged With Fraud
2011-09-16 07:42:00
After losing $2B in unauthorized trade.

New Dolphin Species Discovered
2011-09-16 07:09:00
In Australian bay.

Rogue Trader Told Bank of Error
2011-09-16 05:16:00
UBS hadn't detected the $2B loss.

Casey Anthony Must Pay $97K
2011-09-16 05:14:00
To reimburse law enforcement for search.

FAA Shutdown Averted
2011-09-16 05:10:00
Thousands would have been furloughed Saturday.
$