маємо файл file.txt
# cat file.txt
this is simple text file
here is 1-st line
4-th line
some 3-rd line
end of file
Наприклад, треба замінити стрічку яка містить "4-th line" . Зробити це можна за допомогою sed-у:
# cat file.txt | sed '/4-th line/c 2-nd line'
this is simple text file
here is 1-st line
2-nd line
some 3-rd line
end of file
Але треба памятати, що sed міняє сам файл у такому випадку. Тому, якщо треба змнити файл, можна зробити шось таке:
# cat file.txt | sed '/4-th line/c 2-nd line' > tmp.txt && mv tmp.txt file.txt
таким вийшов новий файл
# cat file.txt
this is simple text file
here is 1-st line
2-nd line
some 3-rd line
end of file
Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
Friday, March 29, 2013
Tuesday, November 1, 2011
how to: python mongodb map-reduce
it's simple example how-to use map-reduce in mongo with python script
use:
use:
- python3
- pymongo3 library
import pymongofrom datetime import datetime, timedeltafrom pymongo.code import CodeMAP_FUNCTION = Code("function () { emit(this.player, { count:1, score:this.score} ); }")REDUCE_FUNCTION = Code("function (key, values) {var result = {count:0, score:0}; values.forEach(function (value) {result.count += value.count; result.score += value.score;}); return result; }")dbhost="" dbport=27017mongodb = pymongo.Connection(dbhost, dbport)db = mongodb.age=10query={'last_visited': {'$gte': datetime.utcnow()-timedelta(age)}}result = db..map_reduce(map=MAP_FUNCTION, reduce=REDUCE_FUNCTION, out="output", query=query)
for item in result.find():player = item["_id"]score = item["value"]['score']print(player, score)
Labels:
database,
mongo,
programming,
python
Subscribe to:
Posts (Atom)