Inserting text into a file with awk or sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inserting text into a file with awk or sed
# 1  
Old 04-12-2013
Code Inserting text into a file with awk or sed

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!
# 2  
Old 04-12-2013
Your script replaces line 4 of the input instead of adding lines before it. Try something like:
Code:
awk 'NR == 4 {$0 = "BEGIN:VtMEZONE\n" $0} 1' $TMPFILE

to insert a line of text before line 4.
# 3  
Old 04-12-2013
When adding the following into the script:
Code:
##### Adding specific ICAL data
awk 'NR == 4 {$0 = "BEGIN:VTIMEZONE\n" $0} 1' $TEMPFILE
awk 'NR == 5 {$0 = "TZID:Asia/Singapore\n" $0} 1' $TEMPFILE

The output on the shell looks like this:
Code:
bash$ ./weather.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  4122    0  4122    0     0   6696      0 --:--:-- --:--:-- --:--:-- 10992
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VTIMEZONE
BEGIN:VEVENT
DTSTAMP:20130412T000000Z
DTSTART;VALUE=DATE:20130412
SUMMARY:Thunderstorm 32C / 26C 
END:VEVENT
END:VCALENDAR
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
TZID:Asia/Singapore
DTSTAMP:20130412T000000Z
DTSTART;VALUE=DATE:20130412
SUMMARY:Thunderstorm 32C / 26C 
END:VEVENT
END:VCALENDAR

And the generated .ics file only has the basic data in it:
Code:
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20130412T000000Z
DTSTART;VALUE=DATE:20130412
SUMMARY:Thunderstorm 32C / 26C 
END:VEVENT
END:VCALENDAR

# 4  
Old 04-12-2013
Try this as a complete replacement for your script:
Code:
#!/bin/bash
##### Begin
DATE=$(date +'%Y%d%m')

##### Fetching weather data, adding specific calendar data, and cleaning up
(if curl http://ical.wunderground.com/auto/ical/global/stations/48698.ics?units=metric 2>$DATE.err
then    rm $DATE.err
else    cat $DATE.err >&2
        rm $DATE.err $DATE.ics
        printf "ABORT\n"
fi)| awk '
$1 == "ABORT" { exit 1}
{       gsub("\r", "")}
NR==1 || NR==5 || NR==6 || NR==10 || NR==17 || NR==18 || NR==22 || NR==24 || NR==135
NR==7 { printf("%s", "BEGIN:VTIMEZONE\n" \
                "TZID:Asia/Singapore\n" \
                "BEGIN:DAYLIGHT\n" \
                "TZOFFSETFROM:+0700\n" \
                "DTSTART:19330101T000000\n" \
                "TZNAME:GMT+08:00\n" \
                "TZOFFSETTO:+0720\n" \
                "RDATE:19330101T000000\n" \
                "END:DAYLIGHT\n" \
                "BEGIN:STANDARD\n" \
                "TZOFFSETFROM:+0730\n" \
                "DTSTART:19820101T000000\n" \
                "TZNAME:GMT+08:00\n" \
                "TZOFFSETTO:+0800\n" \
                "RDATE:19820101T000000\n" \
                "END:STANDARD\n" \
                "END:VTIMEZONE\n")}' > $DATE.ics

This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

2. Shell Programming and Scripting

awk/sed inserting muliple informations in one column

I'm probably sure I need to use either awk or sed judging by research but I'm not sure what exact command I have to do to do following...:wall: So this is my text file CPU 1 2 3 4 5 6 RAM 2 3 4 5 6 7 HAR 3 4 5 6 7 8 -------------- my input: Cur_CPU=10 Cur_RAM=11 Cur_HAR=13 Desired... (5 Replies)
Discussion started by: simonirang
5 Replies

3. Shell Programming and Scripting

Inserting text into a new file

Hi all, I want to create a file and then insert some text into it. I'm trying to create a .sh script that will create a new python file from a template. Can someone tell me why this won't work, touch $1 | sed -e '1i\Some test code here' Sorry I'm quite new to all this! Just as a side... (3 Replies)
Discussion started by: ahodgson
3 Replies

4. UNIX for Dummies Questions & Answers

Inserting a column into a text file

I have a tab delimited text file with multiple columns (data.txt). I would like to insert a column into the text file. The column I want to insert is in a text file (column.txt). I want to insert it into the 5th column of data.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

awk or sed to format text file

hi all, i have a text file which looks like the below 01 02 abc Top 40 music Kidz Only! MC 851 MC 852 MC 853 7NOW Arch_Diac xyz2 abc h211 Commacc1 Commacc2 Commacc3 (4 Replies)
Discussion started by: posner
4 Replies

6. Shell Programming and Scripting

Inserting text with SED

Hi guys, I need to insert @test.com after each entry in my .txt file. 1 2 3 4 1@test.com 2@test.com 3@test.com 4@test.com Tried to use cat test.txt |sed 's/$/@test.com/'but it does this instead: 1 @test.com 2 (6 Replies)
Discussion started by: spirm8
6 Replies

7. Shell Programming and Scripting

Inserting text to file, sed and variable not acting right

I want to put text stored in a variable into a file on the 7th line. I'm having trouble with this line: sed '7i\'$text'' $file It works perfectly for a single word, but fails if there are two words because of the space. I've tried several forms of quoting and this is the only one that... (2 Replies)
Discussion started by: fubaya
2 Replies

8. Shell Programming and Scripting

inserting a string to a text file

Hello Can somebody please help me with the following script? I'm trying to create a text file with 20 blank lines and then insert a string in line 2 but nothing is printed in the itxtfile. I can create the file with 20 blank lines but when I "tell" it to print something on the second line, it... (4 Replies)
Discussion started by: goude
4 Replies

9. Shell Programming and Scripting

Inserting text and modifying the file

I am in a dire need of doing this job , please help from shell script or perl script. It will be highly appreciated. Please have a look at the following INPUT file; The first 14 rows are not of interest but I want them to be included in the output file as they are. From the row 14... (3 Replies)
Discussion started by: digipak
3 Replies

10. Shell Programming and Scripting

Question about sed. Inserting text in field?

Hi, I have tried to develop a sed script that inserts date and time in the third field in the first and second row below. The third row is an example and it shows where the date and time should be inserted. The script should check if the row already has date and time in the third field and if it... (2 Replies)
Discussion started by: pcrs
2 Replies
Login or Register to Ask a Question