Suppose there is some text from a file:
(bookmarks
("Chapter 1 Introduction 1" "#1"
("1.1 Problem Statement and Basic Definitions 23" "#2")
("Exercises 31" "#30")
("Notes and References 42" "#34"))
)
I want to add 11 to each number followed by a "
in each line if there is one, ie
(bookmarks
("Chapter 1 Introduction 12" "#12"
("1.1 Problem Statement and Basic Definitions 34" "#13")
("Exercises 42" "#41")
("Notes and References 53" "#45"))
)
Here is my solution by using GNU AWK and regex:
awk -F'#' 'NF>1{gsub(/"(\d+)\""/, "\1+11\"")}'
i.e., I want to replace (\d+)\"
with \1+10\"
, where \1
is the group representing (\d+)
. But it doesn't work. How can I make it work?
If gawk is not the best solution, what else can be used?
Best Answer
Try this (gawk is needed).
Test with your example:
Note that this command won't work if the two numbers (e.g. 1" and "#1") are different. or there are more numbers in same line with this pattern (e.g. 23" ...32"..."#123") in one line.
UPDATE
Since @Tim (OP) said the number followed by
"
in same line could be different, I did some changes on my previous solution, and made it work for your new example.BTW, from the example I feel that it could be a table of content structure, so I don't see how the two numbers could be different. First would be the printed page number, and 2nd with # would be the page index. Am I right?
Anyway, you know your requirement best. Now the new solution, still with gawk (I break the command into lines to make it easier to read):
test with your new example:
EDIT2 based on @Tim 's comment
You are right for the separator in both input and output part. It defined separator as:
There are two double quotes, because it is easier to catch the two numbers you want (based on your example input).
Exactly!
This is from http://www.gnu.org/s/gawk/manual/html_node/String-Functions.html. you can read to get detailed usage of gensub.