Numerical Labeling using sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Numerical Labeling using sed
# 1  
Old 04-26-2010
Numerical Labeling using sed

Hi,

I have been working on this problem, but could only get so far. I have a file that looks like this

Code:
(cat,chimp,(((dog,cat,cow),orangutan),((horse,((cat,dog),(cow,pig))),cat,mouse,rat)));

I would like after each instance of the word 'cat' to have an incrementing numerical label.

I would like my end file to look like this

Code:
(cat1,chimp,(((dog,cat2,cow),orangutan),((horse,((cat3,dog),(cow,pig))),cat4,mouse,rat)));

so far i have tried this
Code:
sed -e 's/cat/cat#1/' -e 's/cat/cat#2/'  FileIn > Fileout

which places #1 & #2 after the first cat, and ignores the rest.

I then tried this
Code:
sed '
/cat/ a\
1
' FileIn> Fileout

which was even more useless as it just placed a 1 at the end of my string. If anyone has any suggestions or tips to put me in the right direction i would very much appreciate it.

Thank you
# 2  
Old 04-26-2010
Code:
echo '(cat,chimp,(((dog,cat,cow),orangutan),((horse,((cat,dog),(cow,pig))),cat,mouse,rat)));' | nawk -F'cat' '{for(i=1;i<=NF;i++) printf("%s%s", $i, (i==NF)?ORS:FS i)}'

# 3  
Old 04-26-2010
Perl One Liner to Count Occurence of String and append Count to string

If you want an ever-increasing count for the whole file.

Code:
perl -ple 's/(cat)/$1.++$i/eg' in > out

If you want the string count to reset after the end of each line:

Code:
perl -ple 's/(cat)/$1.++$i/eg; $i=0' in > out

Adding zero-width word boundaries will make sure you don't count the "cat" in strings like "category", or "altercation"

Code:
perl -ple 's/\b(cat)\b/$1.++$i/eg' in > out

Hope that Helps

Last edited by deindorfer; 04-26-2010 at 09:53 PM..
# 4  
Old 04-27-2010
Not as generic as awk, but this sed would work too:
Code:
sed 's/cat/&1/1;s/cat/&2/2;s/cat/&3/3;s/cat/&4/4'

or
Code:
sed -e 's/cat/&1/1' -e 's/cat/&2/2' -e 's/cat/&3/3' -e 's/cat/&4/4'

# 5  
Old 04-27-2010
Numerical Labeling using sed

Dear All,

Thank you so much, i wasnt expecting so many replies on this. Really enjoyed the three different options here, awk, perl and sed.

Thanks again,
# 6  
Old 04-27-2010
Quote:
Originally Posted by vgersh99
Code:
echo '(cat,chimp,(((dog,cat,cow),orangutan),((horse,((cat,dog),(cow,pig))),cat,mouse,rat)));' | nawk -F'cat' '{for(i=1;i<=NF;i++) printf("%s%s", $i, (i==NF)?ORS:FS i)}'

nice one
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Solaris

EFI disk labeling / understand the parition table / sectors not continue

Hi all, I have a EFI disk and it is use in zfs pool. partition> p Volume: rpool Current partition table (original): Total disk sectors available: 1172107117 + 16384 (reserved sectors) Part Tag Flag First Sector Size Last Sector 0 usr wm ... (8 Replies)
Discussion started by: javanoob
8 Replies

2. Shell Programming and Scripting

PHP Labeling/Punctuation Syntax Question

Greetings! My first PHP question; and, no doubt, a "no-brainer" for the initiated :) The question centers around the proper syntax for input field labeling. The snippet which puzzles me (and the candidate which I wish to modify) goes like this:<?php _e('Hello World'); ?>:<br />What I'd like... (0 Replies)
Discussion started by: LinQ
0 Replies

3. UNIX for Dummies Questions & Answers

Validate numerical

friends that way I can validate that the value is numeric taken instance var = 12re if var = numeric then a echo "Numerical else echo "not mumerico" fi edit by bakunin: please use CODE-tags for code, data and terminal output (yourself). (9 Replies)
Discussion started by: tricampeon81
9 Replies

4. Solaris

Labeling VTOC

How to relabeling the disk VTOC if i relabel the disk the previous data is available or not (5 Replies)
Discussion started by: chandu.bezawada
5 Replies

5. UNIX for Dummies Questions & Answers

Can grep do numerical comparisons?

Say for example I have a list of numbers.. 5 10 13 48 1 could I use grep to show only those numbers that are above 10? For various reasons I can only use grep... not awk or sed etc. (7 Replies)
Discussion started by: Uss_Defiant
7 Replies

6. UNIX for Dummies Questions & Answers

how to get the value of a numerical expression

Hi, All, I want to calculate a specific value of a Gaussian distribution, say the mean is a=3, variance is b=5, the indepentent variable is x=2, how could I get the y which is the Gaussian distribution value of x. Thanks, Jenny (1 Reply)
Discussion started by: Jenny.palmy
1 Replies

7. Solaris

Automatic disk labeling

First post :) ... Here is a script for automatic labeling of previously unlabeled disks. Other methods exist (format -f cmd_file), but I like this because it's all in one place. #!/bin/ksh #---------------------- # format_label # Automatic labeling of previously unlabeled disks #... (1 Reply)
Discussion started by: kwachtler
1 Replies

8. Shell Programming and Scripting

Numerical Decision

I'm tryning to do something like this, I have this file: spaces12tabgoodbye spaces3tabhello I want to copy to another file the lines that have the number above 10... I thought using sort -rn but I don't know how to discard the lines that have the number below 10. Any idea? Thanks (3 Replies)
Discussion started by: pmpx
3 Replies
Login or Register to Ask a Question