Author Topic: Help With Sed  (Read 2388 times)

daniel3000

  • Hero Member
  • *****
  • Posts: 1003
    • View Profile
    • http://
Help With Sed
« on: September 15, 2006, 05:27:38 am »
Hello,

sorry for the slight off-tpic posting but I'm converting my KO/PI journals now to text files which I will continue to maintain with nedit.

I have defined the steps for conversion already and found solution to all conversion issues, just one is too hard for me.
If someone can help me solve it I'll be able to provide a shell script which converts KO/PI exported journals to nice looking text files which are easily maintainable using nedit with a few macros.

I have a string like

Code: [Select]
==== Montag 31 Juli 2006
(Montag = Monday, any week day is possible here)

and need to convert it to

Code: [Select]
==== 31. Juli 2006 (Mon)
or even better to

Code: [Select]
==== 31.07.2006 (Mon)

i.e. cut the week day, copy the day number, convert the month name to a month number and print it, copy the year, and then putting the first three letters of the week day into brackets.

I'm sure this can be done with sed, but my knowledge is too limited and reading/understanding the tutorials would take me ages.

Could someone please help?

Thanks!
daniel
SL-C3200 with weeXpc, based on pdaXrom 1.1.0beta3
HP 200LX with MS-DOS 5.0

zmiq2

  • Sr. Member
  • ****
  • Posts: 383
    • View Profile
    • http://
Help With Sed
« Reply #1 on: September 15, 2006, 05:48:09 am »
Hi,

apart from sed, I'd suggest yu take a look at awk/gawk, which I find very useful.

HTH
sl-c750, archos av580, socket cf [bt, wifi, modem], noname cf lan, audiovox rtm800 gsm-gprs cf, rom: sharp -> oz3.5.3 -> cacko -> oz3.5.4.1

daniel3000

  • Hero Member
  • *****
  • Posts: 1003
    • View Profile
    • http://
Help With Sed
« Reply #2 on: September 15, 2006, 06:41:02 am »
Quote
Hi,

apart from sed, I'd suggest yu take a look at awk/gawk, which I find very useful.

HTH
[div align=\"right\"][a href=\"index.php?act=findpost&pid=141469\"][{POST_SNAPBACK}][/a][/div]

Solved, thanks! With awk and some trial and error I have found a working solution!
Will post it publicly soon!

daniel
SL-C3200 with weeXpc, based on pdaXrom 1.1.0beta3
HP 200LX with MS-DOS 5.0

Da_Blitz

  • Hero Member
  • *****
  • Posts: 1579
    • View Profile
    • http://www.pocketnix.org
Help With Sed
« Reply #3 on: September 17, 2006, 04:23:08 am »
try giving the date command a go

specifically look at the -d flag


and here you go:
Code: [Select]
$ date -d "Monday 31 july 2006" +"%d.%m.%Y (%a)"
31.07.2006 (Mon)
Personal Blog
Code
Twitter

Gemini Order: #95 (roughly)
Current Device: Samsung Chromebook Gen 3
Current Arm Devices Count: ~30
Looking to acquire: Cavium Thunder X2 Hardware

daniel3000

  • Hero Member
  • *****
  • Posts: 1003
    • View Profile
    • http://
Help With Sed
« Reply #4 on: September 17, 2006, 03:27:43 pm »
Quote
try giving the date command a go

specifically look at the -d flag


and here you go:
Code: [Select]
$ date -d "Monday 31 july 2006" +"%d.%m.%Y (%a)"
31.07.2006 (Mon)
[div align=\"right\"][a href=\"index.php?act=findpost&pid=141611\"][{POST_SNAPBACK}][/a][/div]

Didn't even consider that the date command could do this for me. Great idea, thanks! With a relatively clumsy solution based on awk I have solved that issue already and converted my journals to the new nedit-based format.

Here is the script I used to convert the KO/PI-exported journals (German language settings in KO/PI!) to a format suitable for easy reading and maintenance using nedit:

Code: [Select]
#!/bin/bash

# This converts journals exported from KO/PI (German version)
# to simple text journals maintainable with nedit
# and a simple nedit macro

#publish this in OESF 21456!

infile="$1"
outfile="../journale/$1"

echo Converting $infile to $outfile

sed -e "1,2 d" $infile | \
grep -v "^\*\*\*\*\*\*\*\*\*" | \
grep -v "^Zuletzt ge" | \
sed -e "s/^Description:/\n\n/" | \
sed -e "s/^Journal:.* vom /@@DATELINE@@ /" | \
awk 'BEGIN { FS=" "; Monat=0} /^@@DATELINE@@/ \
{ \
if ($4=="Januar") Monat=1; \
else if ($4=="Februar") Monat=2; \
else if ($4=="März") Monat=3; \
else if ($4=="April") Monat=4; \
else if ($4=="Mai") Monat=5; \
else if ($4=="Juni") Monat=6; \
else if ($4=="Juli") Monat=7; \
else if ($4=="August") Monat=8; \
else if ($4=="September") Monat=9; \
else if ($4=="Oktober") Monat=10; \
else if ($4=="November") Monat=11; \
else if ($4=="Dezember") Monat=12; \
else Monat=99;\
printf("\n\n==== %s.%02d.%s (%.3s)",$3,Monat,$5,$2) \
} \
!/@@DATELINE@@/ {print}' > $outfile

And here is some example:

KO/PI journal:

Code: [Select]
KO/Pi Description/Journal save file.
Save date: Freitag 15 September 2006 10:12
************************************
Journal: Meeting RNS vom Freitag 09 September 2005
Zuletzt geändert: Donnerstag 23 Februar 2006 12:53
Description:
This is the text of journal entry no 1.
Just an example, no private data included here!

************************************
Journal: Meeting RNS vom Dienstag 13 September 2005
Zuletzt geändert: Donnerstag 23 Februar 2006 12:53
Description:
This is the text of journal entry no 2.
Just another example, no private data included here!

Converted:

Code: [Select]
==== 09.09.2005 (Fre)

 
This is the text of journal entry no 1.
Just an example, no private data included here!


==== 13.09.2005 (Die)

 
This is the text of journal entry no 2.
Just another example, no private data included here!

And these are the nedit macros I use to maintain the journals now (need some optimization, esp. Alt-B and Alt-F):

Code: [Select]
Journal_NewEntry:Alt+N::: {\n\
end_of_file()\n\
d="\\n\\n==== " shell_command("date +\\"%d.%m.%Y (%a) %R\\"", "") "\\n\\n"\n\
insert_string(substring(d, 0, length(d)-1))\n\
}\n\
Journal_GotoLastEntry:Alt+V::: {\n\
end_of_file()\n\
set_cursor_pos(search("==== ",$cursor,"backward"))\n\
}\n\
Journal_GoBackwards:Alt+B::: {\n\
beginning_of_line()\n\
key_select("left")\n\
 set_cursor_pos(search("==== ",$cursor,"backward"))\n\
}\n\
Journal_GoForward:Alt+F::: {\n\
end_of_line()\n\
set_cursor_pos(search("==== ",$cursor,"forward"))\n\
scroll_down(15)\n\
}\n

quite convenient, and better than the KO/PI journal feature!

daniel
« Last Edit: September 17, 2006, 04:48:53 pm by daniel3000 »
SL-C3200 with weeXpc, based on pdaXrom 1.1.0beta3
HP 200LX with MS-DOS 5.0

Da_Blitz

  • Hero Member
  • *****
  • Posts: 1579
    • View Profile
    • http://www.pocketnix.org
Help With Sed
« Reply #5 on: September 18, 2006, 12:40:12 am »
it is truly amazing what some of the commands can do, most people dont know the full capabilities of simple programs such as date or cat or ls
Personal Blog
Code
Twitter

Gemini Order: #95 (roughly)
Current Device: Samsung Chromebook Gen 3
Current Arm Devices Count: ~30
Looking to acquire: Cavium Thunder X2 Hardware