Tag Archives: TEMPFILE

Inserting text into a file with awk or sed

By Schubi

Hello,

I’ve been trying to get a script working that fetches weather-data and converts it into an .ics file. The script works so far put I’m stuck at the point where I need to add specific static data. A thorough search through the forum did not point me into the right direction.

Code:

#!/bin/bash
##### Begin
DATE=$(date +'%Y%d%m')
TIME=$(date +'%H:%M:%S')
TEMPFILE="$(mktemp temp.XXX)"

if [ $? -ne 0 ]; then
echo "$0: Can't create temp file, exiting..."
exit 1
fi

##### Fetching weather data
curl http://ical.wunderground.com/auto/ical/global/stations/48698.ics?units=metric | awk 'NR==1 || NR==5 || NR==6 || NR==10 || NR==17 || NR==18 || NR==22 || NR==24 || NR==135' > $TEMPFILE

##### Adding specific calendar data

##### Cleaning up
cat $TEMPFILE | tr -d "r" > $DATE.ics
rm $TEMPFILE
exit 0


The output is as expected a .ics file that can be imported into a calendar:

Code:

BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20130412T000000Z
DTSTART;VALUE=DATE:20130412
SUMMARY:Thunderstorm 32C / 26C
END:VEVENT
END:VCALENDAR


In addition to the above output, the following data set needs to be imported between lines 3 and 4:

Code:

BEGIN:VTIMEZONE
TZID:Asia/Singapore
BEGIN:DAYLIGHT
TZOFFSETFROM:+0700
DTSTART:19330101T000000
TZNAME:GMT+08:00
TZOFFSETTO:+0720
RDATE:19330101T000000
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:+0730
DTSTART:19820101T000000
TZNAME:GMT+08:00
TZOFFSETTO:+0800
RDATE:19820101T000000
END:STANDARD
END:VTIMEZONE


I’ve been playing around with the following string but did not manage to produce a result (example):
awk ‘NR==4{$0=”BEGIN:VTIMEZONE”}1′ $TEMPFILE

Any help is appreciated!

From: http://www.unix.com/unix-dummies-questions-answers/221013-inserting-text-into-file-awk-sed.html

Variable on If Statement

By The OneHi,

I am tasked to modify soem script and I come accross a line which I dont fully understand. I tried searching online but I couldnt get a good explanation on it.

Here it the part of the code:

Code:

PAY_RT=`cat $TEMPFILE | cut -f2 -d”,”`

if [ ${PAY_RT:-BLANK = ‘P’ ];
then
PAY_RT=R
fi
What is the colon (:) and hypen (-) means on the if statement? I never used this on any of my if statement before.

There are also variation of :

Code:
if [ ${PAY_AMNT:-“00” = “000” ];
then
PAY_AMNT=00
fi

Source: The UNIX and Linux Forums