Replacing end of line characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing end of line characters
# 1  
Old 04-22-2014
Tools Replacing end of line characters

Hello,

I'm looking for some help in renaming data-timestamps stored within different calendar directories/files.

The calendar directory has hundreds of ICS files:
Code:
~/Library/Calendars/F494C908.calendar/Events/92B65E439AAC.ics
~/Library/Calendars/F494C908.calendar/Events/DE7867E61969.ics
~/Library/Calendars/KB67E731.calendar/Events/40bb3f34b060.ics
~/Library/Calendars/KB67E731.calendar/Events/901DB10BBCD3.ics
~/Library/Calendars/86SC23B7.calendar/Events/c7d53a321400.ics
...

A single ICS file usually holds the following example data:
Code:
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
BEGIN:VEVENT
TRANSP:OPAQUE
DTEND;TZID=Europe/London:20131017T154059
DTSTAMP:20140405T115011Z
LOCATION:London
DESCRIPTION:Test
SEQUENCE:10
SUMMARY:Test
LAST-MODIFIED:20131017T122550Z
DTSTART;TZID=Europe/London:20131017T151512
CREATED:20131017T122550Z
END:VEVENT
END:VCALENDAR

My goal is to rename any DTSTART/DTEND line in all directories and files that doesn't end with 00 to end (replace) with 00
DTSTART;TZID=Europe/London:20131017T151512
should become
DTSTART;TZID=Europe/London:20131017T151500
as well as
DTEND;TZID=Europe/London:20131017T154059
should become
DTEND;TZID=Europe/London:20131017T154000


So far my command does the following:

1) Find .ICS files in directory
2) Only use lines that have DTSTART or DTEND in it
3) Remove the OSX line ending
4) Find lines that don't end with 00

Code:
find ~/Library/Calendars -type f -iname *.ics -exec cat -vte {} \; | egrep -ia 'DTSTART;TZID|DTEND;TZID' | tr -d '^M$' | grep -v '00$'

However, I've been unable to find a way to replace the last two digits to 00 and write it directly to the files. Apparently sed '/00$/!s/..$/00/' does some magic but I don't understand the exact syntax and it also doesn't write it to the files!

Any help is much appreciated!
# 2  
Old 04-22-2014
Try this and make sure you have a backup of all your files:
Code:
find . -type f -iname *.ics -exec sed -i -e '/^DT\(START\|END\)/s/[0-9][0-9]$/00/' -e 's/\r//' {} \;

if the carriage return is not removed, try changing \r with ^M (ctrl+v+m)
# 3  
Old 04-22-2014
Quote:
Originally Posted by Subbeh
Try this and make sure you have a backup of all your files
Thanks for your quick reply. Backups are in place. When I run the command I get:
Code:
sed: -e: No such file or directory
sed: -e: No such file or directory
sed: -e: No such file or directory
...

# 4  
Old 04-22-2014
Which OS and version of sed do you use?

You could try this:
Code:
find . -type f -iname *.ics -exec sed -i '/^DT\(START\|END\)/s/[0-9][0-9]$/00/;s/\r//' {} \;

This User Gave Thanks to Subbeh For This Post:
# 5  
Old 04-22-2014
Also test this out first and try:
Code:
for file in ~/Library/Calendars/*.calendar/Events/*.ics
do
  [ -f "$file" ] || continue
  sed -i ".bak" -e '/^DTSTART/ba' -e'/^DTEND/ba' -e 'p;d' -e :a -e 's/..\(.\)$/00\1/' "$file"
done

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 04-22-2014
Unfortunately I'm forced to do this on OSX Mavericks 10.9 and it looks like the -i option in sed is handled differently - also discussed in this thread: stackoverflow.com/questions/2320564/variations-of-sed-between-osx-and-gnu-linux

@Subbeh: I've been running all four variations without an error but the date/time string stays unchanged - I need to do more testing later as I assume it's still the ^M line ending that breaks it..
Code:
find . -type f -iname *.ics -exec sed -i "" -e '/^DT\(START\|END\)/s/[0-9][0-9]$/00/' -e 's/\r//' {} \;
find . -type f -iname *.ics -exec sed -i "" -e '/^DT\(START\|END\)/s/[0-9][0-9]$/00/' -e 's/^M//' {} \;
find . -type f -iname *.ics -exec sed -i "" '/^DT\(START\|END\)/s/[0-9][0-9]$/00/;s/\r//' {} \;
find . -type f -iname *.ics -exec sed -i "" '/^DT\(START\|END\)/s/[0-9][0-9]$/00/;s/^M//' {} \;

@Scrutinizer: The loop also works when I remove .bak:
Code:
for file in ~/Library/Calendars/*.calendar/Events/*.ics
do
  [ -f "$file" ] || continue
  sed -i "" -e '/^DTSTART/ba' -e'/^DTEND/ba' -e 'p;d' -e :a -e 's/..\(.\)$/00\1/' "$file"
done

# 7  
Old 04-22-2014
Below code will create the filename ".tmp" files with the required changes.
Code:
for i in ~/Library/Calendars/*.calendar/Events/*.ics
do
  awk 'BEGIN{FS = OFS = "T"} {if($0 ~ /^DTSTART/ || $0 ~ /^DTEND/) {$NF = $NF - ($NF % 100)}}1' ${i} > ${i}.tmp
done

If you want to make the changes into the file, use below
Code:
for i in ~/Library/Calendars/*.calendar/Events/*.ics
do
  awk 'BEGIN{FS = OFS = "T"} {if($0 ~ /^DTSTART/ || $0 ~ /^DTEND/) {$NF = $NF - ($NF % 100)}}1' ${i} > ${i}.tmp && mv ${i}.tmp ${i}
done

This User Gave Thanks to SriniShoo 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

sed - Removing all characters from token to end of line

Hello. The token is any printable characters between 2 " . The token is unknown, but we know that it is between 2 " Tok 1 : "1234x567" Tok 2 : "A3b6+None" Tok 3 : "A3b6!1234=@" The ligne is : Line 1 : "9876xABCDE"Do you have any code fragments or data samples in your post Line 2 : ... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

3. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

4. UNIX for Dummies Questions & Answers

Append characters to end of line

Coding in unix shell script. Hi All, Please help Thanks (6 Replies)
Discussion started by: sam12345
6 Replies

5. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

6. UNIX for Dummies Questions & Answers

Inserting control characters at the end of each line

How to add control characters at the end of each line in a file? Can anyone help me with this? Thanks, Shobana (2 Replies)
Discussion started by: Shobana_s
2 Replies

7. Shell Programming and Scripting

Replacing end of line with " in a UNIX file

How should I replace End of line Character by ". i.e in a file - Name1,NO1 Name2,No2 Name3,No3 .... Should look like -- Name1,NO1" Name2,No2" Name3,No3" .... (2 Replies)
Discussion started by: The Observer
2 Replies

8. Shell Programming and Scripting

replacing certain characters with new line?

i have a text file which domains something like this 123213213213/32434342 324324324/12312321321 12321321,435435435 12321312 / 12313213 / 12435435345 4353213 , 123213213213 21321321312-12334324 234324324 - 235645645645 456456456 - 45456456456 - 45645645654243 how can i replace '/' and... (4 Replies)
Discussion started by: Bashar
4 Replies

9. Shell Programming and Scripting

Replacing characters in file with line break

Hi, Apologies if this has been asked before, but I searched and was not able to find an answer. It's probably a simple question to answer for those of you with some experience, though... I have a relatively long string where tokens are separated by the colon (':') character. Let's say the... (10 Replies)
Discussion started by: johnemb
10 Replies

10. Shell Programming and Scripting

Replacing all but last Hex characters in a text line

I must remove hex characters 0A and 0D from several fields within an MS Access Table. Since I don't think it can be done in Access, I am trying here. I am exporting a Table from Access (must be fixed length fields, I think, for my idea to work here) into a text format. I then want to run a... (2 Replies)
Discussion started by: BAH
2 Replies
Login or Register to Ask a Question