如何操纵以下文字

时间:2011-03-15 02:16:18

标签: python regex perl sed awk

我想知道如何转换类似于以下内容的文字:

Chapter 3 Convex Functions 97
3.1 Definitions 98
3.2 Basic Properties 103

为:

("Chapter 3 Convex Functions 97" "#97")
("3.1 Definitions 98" "#98")
("3.2 Basic Properties 103" "#103")

使用一些方便但功能强大的文本操作语言和/或实用程序,如sed,awk,regex,perl,python,......

谢谢和问候!


注意: 在每一行中,重复最后一个数字。

7 个答案:

答案 0 :(得分:5)

这是一个Perl解决方案:

while (<DATA>) {
    s/^(.+ (\d+))$/("$1" "#$2")/;
    print;
}

__DATA__
Chapter 3 Convex Functions 97
3.1 Definitions 98
3.2 Basic Properties 103

打印:

("Chapter 3 Convex Functions 97" "#97")
("3.1 Definitions 98" "#98")
("3.2 Basic Properties 103" "#103")

或作为一个班轮:

perl -pe 's/^(.+ (\d+))$/("$1" "#$2")/'

答案 1 :(得分:2)

在Python中,

"Chapter 3 Convex Functions 97".rsplit(None,1)

给出

['Chapter 3 Convex Functions', '97']

使用一段文字,

txt = """Chapter 3 Convex Functions 97
    3.1 Definitions 98
    3.2 Basic Properties 103"""

for line in txt.split('\n'):
    line = line.strip().rsplit(None,1)
    print('("{0} {1}" "#{1}")'.format(*line))

给出

("Chapter 3 Convex Functions 97" "#97")
("3.1 Definitions 98" "#98")
("3.2 Basic Properties 103" "#103")

修改:我已根据您的注释对其进行了更新,以使页码重复。

答案 2 :(得分:2)

适用于几乎每个版本的python

infile = open("input.txt")
outfile = open("output.txt", "w")

for line in infile:
    line, last = line.rstrip().rsplit(" ", 1)
    outfile.write('("%s %s" "#%s")\n' % (line, last, last))

答案 3 :(得分:2)

以下是使用sed进行此操作的几种方法:

sed 's/\(.* \)\(.*\)/("\1\2" "#\2")/' inputfile

sed 's/\(.* \)\([0-9]*\)/("\1\2" "#\2")/' inputfile

以下是一对使用AWK的人:

awk '{n = $NF; print "(\"" $0 "\" \"#" n "\")"}' inputfile

awk 'BEGIN {q="\x22"} {n = $NF; print "(" q $0 q " " q "#" n q ")"}' inputfile

答案 4 :(得分:1)

import re
def format(str):
  m = re.search('(.*)\s(\d+)$', str)
  return "(\"" + m.group(1) + "\" \"#" +  m.group(2) + "\")"

print format('Chapter 3 Convex Functions 97')

print format('3.1 Definitions 98')

print format('3.2 Basic Properties 103')

返回

("Chapter 3 Convex Functions" "#97")
("3.1 Definitions" "#98")
("3.2 Basic Properties" "#103")

答案 5 :(得分:1)

def munge(line):
    number = line.rsplit(None,1)[1]
    return '''("{0}" "#{1}")'''.format(line, number)

答案 6 :(得分:1)

import re
pat = re.compile('^(.+?(\d+)) *$',re.M)

ch = '''Chapter 3 Convex Functions 97 
3.1 Definitions 98  
3.2 Basic Properties 103'''

print ch
print
print pat.sub('"\\1" "#\\2"',ch)

结果

Chapter 3 Convex Functions 97 
3.1 Definitions 98  
3.2 Basic Properties 103

"Chapter 3 Convex Functions 97" "#97"
"3.1 Definitions 98" "#98"
"3.2 Basic Properties 103" "#103"