Replace groups into sequential numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace groups into sequential numbers
# 1  
Old 04-30-2015
Replace groups into sequential numbers

I have a file that looks like this:
Code:
n1 1
n2 1
n3 1
n4 3
n4 3
n2 5
n2 5
n2 5
n2 5
n3 5
n3 5
n4 6
n7 6

that is a name followed be a descriptive number.
I want to make these numbers sequential starting from 0 but without changing the "neighbours" each name belongs to. So the above should be:

Code:
n1 0
n2 0
n3 0
n4 1
n4 1
n2 2
n2 2
n2 2
n2 2
n3 2
n3 2
n4 3
n7 3

Any comments?
# 2  
Old 04-30-2015
Hello FelipeAd,

Following may help you in same.
Code:
awk -vA=-1 'BEGIN{C=$2} {if($2!=C && $2 !=Z){Z=$2;A++;$2=A;} else {$2=A};print $0;C=$2}'  Input_file

Output will be as follows.
Code:
n1 0
n2 0
n3 0
n4 1
n4 1
n2 2
n2 2
n2 2
n2 2
n3 2
n3 2
n4 3
n7 3

Thanks,
R. Singh
# 3  
Old 04-30-2015
Try:
Code:
awk '$2!=p""{c++; p=$2} {print $1, c-1}' file

# 4  
Old 05-04-2015
Quote:
Originally Posted by RavinderSingh13
Hello FelipeAd,

Following may help you in same.
Code:
awk -vA=-1 'BEGIN{C=$2} {if($2!=C && $2 !=Z){Z=$2;A++;$2=A;} else {$2=A};print $0;C=$2}'  Input_file

Output will be as follows.
Code:
n1 0
n2 0
n3 0
n4 1
n4 1
n2 2
n2 2
n2 2
n2 2
n3 2
n3 2
n4 3
n7 3

Thanks,
R. Singh
Thanks. It works, although if the initial data actually begins with 0, it replaces it with -1 which is not desired. The rest works great though

---------- Post updated at 04:02 AM ---------- Previous update was at 04:01 AM ----------

Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk '$2!=p""{c++; p=$2} {print $1, c-1}' file

It works great in all cases, thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Dummies Questions & Answers

[Solved] awk solution to add sequential numbers based on a word

Hi experts, I've been struggling to format a large genetic dataset. It's complicated to explain so I'll simply post example input/output $cat input.txt ID GENE pos start end blah1 coolgene 1 3 5 blah2 coolgene 1 4 6 blah3 coolgene 1 4 ... (4 Replies)
Discussion started by: torchij
4 Replies

3. Shell Programming and Scripting

Perl newbie - regex replace all groups issue

Hello, Although I have found similar questions, I could not find advice that could help with my problem. The issue: I am trying to replace all occurrences of a regex, but I cannot make the regex groups work together. This is a simple input test file: The Vedanta Philosophy... (3 Replies)
Discussion started by: samask
3 Replies

4. Shell Programming and Scripting

Sequential numbers

Hi All, I am looking for a simple way to write numbers to a file sequentially starting from 1 and ending on a specified upper limit. Example of the output file is below Example 1 2 3 4 5 . . . . 1000 please let me know the best way to do it. (10 Replies)
Discussion started by: Lucky Ali
10 Replies

5. Shell Programming and Scripting

Rename files in sub directories with sequential numbers

I can rename a file with sequential numbers from 1 to N with this script: num=1 for file in *.dat;do mv "$file" "$(printf "%u" $num).txt" let num=num+1 done The script begins with renaming a some.dat file to 1.dat.txt and goes on sequentially renaming other DAT files to... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Programming

Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX

Writing a Tool to simulate non-sequential disk I/O (simulate db file sequential read) in C POSIX I have over the years come across the same issue a couple of times, and it normally is that the read speed on SAN is absolutely atrocious when doing non-sequential I/O to the disks. Problem being of... (7 Replies)
Discussion started by: vrghost
7 Replies

7. Shell Programming and Scripting

Shell Scripts (Renaming file names with sequential numbers)

Hi there, Firstly, I have no experience with shell scripts so would really appreciate some help. I have the following shell script that is causing some problems: moveit() { && set -x if then DOUBLE_DELIVERY=$(grep... (6 Replies)
Discussion started by: thebeno
6 Replies

8. UNIX for Dummies Questions & Answers

Replace US numbers with European numbers

hey, I have a file with numbers in US notation (1,000,000.00) as well as european notation (1.000.000,00) i want all the numbers to be in european notation. the numbers are in a text file, so to prevent that the regex also changes the commas in a sentence/text i thought of: sed 's/,/\./'... (2 Replies)
Discussion started by: FOBoy
2 Replies

9. Programming

Reading special characters while converting sequential file to line sequential

We have to convert a sequential file to a 80 char line sequential file (HP UX platform).The sequential file contains special characters. which after conversion of the file to line sequential are getting coverted into "new line" or "tab" and file is getting distorted. Is there any way to read these... (2 Replies)
Discussion started by: Rajeshsu
2 Replies

10. UNIX for Dummies Questions & Answers

inserting uniq sequential numbers at the start of the file

Hi Unix gurus, I have a file. I need to insert sequential number at the starting of the file. Fields are delimited by "|". I know the starting number. Example: File is as follows |123|4test|test |121|2test|test |x12|1test|test |vd123|5test|test starting number is : 120 ... (7 Replies)
Discussion started by: jingi1234
7 Replies
Login or Register to Ask a Question