Rename values in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename values in file
# 1  
Old 06-24-2015
Rename values in file

Dear all,

I have a problem that i cannot solve and I hope someone here can help me.

I have a huge tab delimited text file with two columns in relation. The values in the first column can be redundant and one copy of that value can exist in the second column.

My question is how to simplify the nomenclature of the values and create an output described below.

Input File:
Code:
1000 1000
1000 1100
1000 1200
1100 1300
1200 1500
1200 1700

Output File:
Code:
Node1 Node1
Node1 Node2
Node1 Node3
Node2 Node4
Node3 Node5
Node3 Node6

Alternative Output File:
Code:
Node1 Node1,Node2,Node3
Node2 Node4
Node3 Node5,Node6

I would be pleased to receive any help or suggestions on the issue.

Thanks,
Laszlo
# 2  
Old 06-24-2015
Hello Laslzo,

Following codes may help you in same.
Code:
awk -vNODE=1000 '{for(i=1;i<=NF;i++){DIFF=$i-NODE;if(DIFF>=0){$i="NODE" DIFF/100+1}}} 1'  Input_file
  
NODE1 NODE1
NODE1 NODE2
NODE1 NODE3
NODE2 NODE4
NODE3 NODE6
NODE3 NODE8

2nd code is as follows.
Code:
awk '{A[$1]=A[$1]?A[$1] "," $NF:$NF} END{for(i in A){print i OFS A[i]}}'  Input_file
  
NODE1 NODE1,NODE2,NODE3
NODE2 NODE4
NODE3 NODE6,NODE8

NOTE: You haven't mentioned logic of converting them to NODE string so I have taken a logic starting from 1000 it is NODE1 and increasing 100 to each digit it will increase NODE number too, just for clarification I am adding this.


Thanks,
R. Singh

Last edited by RavinderSingh13; 06-24-2015 at 08:13 AM.. Reason: Added a note for solution
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-24-2015
I can't see a linear relationship between the numbers input and the node numbers assigned:
Code:
1000 --> 1
1100 --> 2
1200 --> 3
1300 --> 4
1500 --> 5
1700 --> 6

Is it possibly the order of appearance according to which numbers are assigned?

---------- Post updated at 13:27 ---------- Previous update was at 13:24 ----------

If so, try
Code:
awk '
!($1 in N)      {N[$1]="NODE"++c}
!($2 in N)      {N[$2]="NODE"++c}
                {print N[$1],N[$2]}
' file
NODE1 NODE1
NODE1 NODE2
NODE1 NODE3
NODE2 NODE4
NODE3 NODE5
NODE3 NODE6

This User Gave Thanks to RudiC For This Post:
# 4  
Old 06-25-2015
Thank you for your answers,

However, I wasn't too explicit.

So, the order of appearance of values in the first column needs to be assigned first to "NODEx" string.

In the first column the values jumps with 1000 steps but it can have gaps.

This is the first swith of column1 values of the current output (pasted to next to the input) with Rudic's solution:
Code:
89000 59041000 NODE1 NODE91
89000 59066000 NODE1 NODE92
90000 260000 NODE93 NODE94
90000 281000 NODE93 NODE95

The desired output would be like this:
Code:
89000 59041000 NODE1 NODE(whatever the 59041000 value in column1 gets)
89000 59066000 NODE1 NODE(whatever the 59066000 value in column1 gets)
90000 260000 NODE2 NODE(whatever the 260000 value in column1 gets)
90000 281000 NODE2 NODE(whatever the 281000 value in column1 gets)

Thank you for your help Smilie

Laszlo
# 5  
Old 06-25-2015
Not clear. How should the 91 in NODE91 be calculated? And how could 90000 be assigned the 93, then, given "the value jumps in 1000 steps", and NODE1 is 89000?
# 6  
Old 06-25-2015
This is just four line that shows "node1" and "node2".
So the first value in column1 (start value=89000) has 92 corresponding "node" (last two shown).
The next value after 89000 (column1) should assigned to "node2" and so on.
The second column number (like 59041000) should inherit it's node name from the first column.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

2. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

5. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

6. UNIX for Dummies Questions & Answers

look for specific values in a file and rename file with value found

Hi, i have a file with some data ..look for some specific value in the file and if found that value rename the file with the value found in the file.. ex.. File.txt 1236 43715825601ANDERSSON, 1236 437158256031963040120060901200609010000000 1236 43715825604123 MCCL AVE UPPER 1236 ... (11 Replies)
Discussion started by: dssyadav
11 Replies

7. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

8. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

9. Shell Programming and Scripting

Replacing values in a file based on values in another file

Hi I have 2 files:- 1. List of files which consists of names of some output files. 2. A delimited file; delimted by "|" I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first... (5 Replies)
Discussion started by: pparthiv
5 Replies

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question