search for any number, and retain them in replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for any number, and retain them in replacement
# 1  
Old 04-29-2009
search for any number, and retain them in replacement

Hello!

Here is what I am trying to do: I am taking a class at school that is using a lot of literature that is in the public domain, and available for free on the web. Rather than read them online in HTML, I have been pulling them down and quickly and dirtily tagging them up and typesetting them with LaTeX.

I am pulling down a piece of work that has a lot of end notes, and i would like to write a script so that I can add all of the \endnotemarker and \endnotetext tags relatively automatically.

All of the notes are already numbered appropriately, but not tagged.

What I would like to do, with either Perl or SED or AWK or VI or whatever would be best... is search for all integers and then apply the tag before an after it. The problem I am running into, is that my search term is something like

Code:
* [0-9]+ #to find in text numbers
^[0-9]+  #to fine end note numbers

but, obviously, if I use a replace term, it is not going to use the number that was found, but literally whatever I write in the replace term (this is all in VI right now, sorry I was not clear).

This is why I think I need some kind of a script that will find the number, store the number in a variable and then I can call upon that specific variable in the replacement term. The variable would update each time a new integer is found.

I might thinking about this all wrong, but can anyone think of a simple way to do this? It is not life and death, it would just make the file I generate something worth keeping around for re-reading.

Thanks!


EDIT: now that I think about it, I dont think this will really work for the LaTeX operation, but does anyone know how to write a script to do this in general... like if I wanted to add a word after every time a number appears in a paragraph, without changing the number?
# 2  
Old 04-29-2009
Code:
echo 'foo 1234 bar fred 56 7890 baz' | sed 's/[0-9][0-9]*/&<myWordHere>/g'


Last edited by vgersh99; 04-30-2009 at 04:27 PM.. Reason: no need for a 'capture'
# 3  
Old 04-30-2009
I'll play with this, thank you! I guess the part of the command that you taught me was the
Code:
&

I did not know of that function! I'll look it up in the manual... thanks!
# 4  
Old 04-30-2009
Tools Another example, defined

Code:
> echo "an apple every 13 days helps 169 people" | sed 's/\([0-9][0-9]*\)/(&)/g'
an apple every (13) days helps (169) people

echo "an apple every 13 days helps 169 people"
input data

| sed
pipe to string editor

's/
substitute

\(
escape before the ( character

[0-9][0-9]*
looking for 2 or more numbers

\)
escape before the closing ) character

/
change to what follows

(&)
here, I am saying I want my # in parenthesis
Note the & carries forward what I found in initial search

/g'
close replacement, tell global (as many as it finds)
# 5  
Old 04-30-2009
actually, we don't even need a 'capture':
Code:
$ echo "an apple every 13 days helps 169 people" | sed 's/[0-9][0-9]*/(&)/g'
an apple every (13) days helps (169) people

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Busybox search replacement for find

Hi, I am looking for a solution to find files in a folder and subfolders. Those files should be moved (some with spaces) when they were between 2015 Feb. 03 and 2014 Nov. 24. find . -type f -newermt "2010-01-01" ! -newermt "2010-06-01" I am working on a QNAP with a small busybox.... (2 Replies)
Discussion started by: krischeu
2 Replies

2. Shell Programming and Scripting

Recursive replacement of search string using sed

Dear Unix Forum Group Members, Please do let me know how I can replace the double pipe with single pipe recursively on single record. Sample Input Data: DN set|Call prefix||| Called number address nature 0||| *789|||||||ALL number types 0||| 00||||||||ALL number types 10||... (5 Replies)
Discussion started by: srinu.kadem
5 Replies

3. Shell Programming and Scripting

"Encrypting" using number replacement

Hi, I have a task of "encrypting" a file by replacing the numbers with another set of numbers. Sort of swapping the values, say: 1 = 5 2 = 8 3 = 7 4 = 1 5 = 9 and so on.. so if i have 12345, my output should be 58719. problem is i get 98719 since after swapping 1 to 5, my sed... (7 Replies)
Discussion started by: agentgrecko
7 Replies

4. UNIX for Dummies Questions & Answers

VIM search and replace with line breaks in both the target and replacement text

Hi, Ive spent ages trying to find an explanation for how to do this on the web, but now feel like I'm :wall: I would like to change each occurence (there are many within my script) of the following: to in Vim. I know how to search and replace when it is just single lines... (2 Replies)
Discussion started by: blueade7
2 Replies

5. UNIX for Advanced & Expert Users

Search and replace a number

a=`grep -i a.sh filename.sh|cut -d "|" -f4` b=`expr $a + 1` filename=`grep -i a.sh filename.sh` while read line do echo $line echo $filename if then echo "entered if" nawk ' BEGIN { FS="|"; OFS="|" } { sub('$a', '$b', $4) print $0}' filename.sh fi echo "exit if" done <... (1 Reply)
Discussion started by: hs.giri
1 Replies

6. Shell Programming and Scripting

Multiple String with a number replacement and more..

Hello all, First of all, I could not made up a nice title what explains my problem in short,sorry for that already. I have the next file which contains the following, CREATE:ENTRY:\ DNAME,"referenceId=sondakika30,referenceId=User1,\ referenceId=Company,\ ... (2 Replies)
Discussion started by: sondakika
2 Replies

7. UNIX for Dummies Questions & Answers

global search and replacement of a non-ascii character

Hi, I need to do a global search and replacement of a non-ascii character. Let me first give the background of my problem. Very frequently, I need to copy set of references from different sources. Typically, a reference would like this: Banumathy et al., 2002 G. Banumathy, V. Singh and U.... (1 Reply)
Discussion started by: effjay
1 Replies

8. Shell Programming and Scripting

sed script for search and replacement

New to scripting.. think i need sed to do the following but need help. have a properties files with variable i need to replace using substitution for e.g in x.properties xslt.root.dir=/apps1/test/cs.war/presentation/xsl have a shell script with following echo "Edit... (3 Replies)
Discussion started by: cookie23patel
3 Replies

9. Shell Programming and Scripting

How to search a number in a certain field

Hello, I have a file that looks like this: num11 num12 num13 word1 num21 num22 num23 word2 num31 num32 num33 word3 . . . I would like to search for the lines that contain a given number, but I need to narrow the search only to the first field, that means that the number has to... (4 Replies)
Discussion started by: shira
4 Replies

10. UNIX for Advanced & Expert Users

Retain Only specific number of file in Directory

I want to retain specific number of backup files in a directory.for example i want to retain only two latest backup file in backup directory. If number of backup files is greater than this policy that it will delete oldest file.Please Tell me whether this is possible or not. (2 Replies)
Discussion started by: ranvijaidba
2 Replies
Login or Register to Ask a Question

Featured Tech Videos