如何操作以下文本

发布于 2024-10-22 03:01:19 字数 389 浏览 1 评论 0原文

类似于以下内容的文本转换为:

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、...)将

谢谢并致以问候!


笔记: 在每一行中,最后一个数字都会重复。

I was wondering how to convert text similar to the following:

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

to:

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

by using some convenient yet powerful text manipulation languages and/or utilities such as sed, awk, regex, perl, python, ...

Thanks and regards!


Note:
In each line, the last number is repeated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

独闯女儿国 2024-10-29 03:01:19

这是一个 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")/'

Here's a Perl solution:

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

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

prints:

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

or as a one liner:

perl -pe 's/^(.+ (\d+))$/("$1" "#$2")/'
金橙橙 2024-10-29 03:01:19

以下是使用 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

Here's a couple of ways to do it using sed:

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

or

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

Here's a couple using AWK:

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

or

awk 'BEGIN {q="\x22"} {n = $NF; print "(" q $0 q " " q "#" n q ")"}' inputfile
咆哮 2024-10-29 03:01:19
import re
def format(str):
  m = re.search('(.*)\s(\d+)

回报

("Chapter 3 Convex Functions" "#97")
("3.1 Definitions" "#98")
("3.2 Basic Properties" "#103")
, 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')

回报

import re
def format(str):
  m = re.search('(.*)\s(\d+)

returns

("Chapter 3 Convex Functions" "#97")
("3.1 Definitions" "#98")
("3.2 Basic Properties" "#103")
, 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')

returns

枕头说它不想醒 2024-10-29 03:01:19
def munge(line):
    number = line.rsplit(None,1)[1]
    return '''("{0}" "#{1}")'''.format(line, number)
def munge(line):
    number = line.rsplit(None,1)[1]
    return '''("{0}" "#{1}")'''.format(line, number)
从来不烧饼 2024-10-29 03:01:19

在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")

编辑:我已根据您的注释对其进行了更新,以便页码重复。

In Python,

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

gives

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

Working with a block of text,

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))

gives

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

Edit: I have updated it per your Note such that the page numbers are duplicated.

合约呢 2024-10-29 03:01:19
import re
pat = re.compile('^(.+?(\d+)) *

结果

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"
,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)

结果

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

result

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"
,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)

result

骷髅 2024-10-29 03:01:19

几乎适用于所有版本的 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))

Works with pretty much every version of 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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文