How do I assign number with some condition?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I assign number with some condition?
# 1  
Old 03-04-2013
How do I assign number with some condition?

My data file looks like this

Code:
location_z    2399    167    1    9    72.92    i    17-Oct-2011    20:11:00    
location_z    2399    167    2    9.75    72.77    i    18-Oct-2011    00:25:00    
location_z    2399    167    3    10.57    72.75    i    18-Oct-2011    18:24:00    
location_b    2400    167    4    10.53    73    i    18-Oct-2011    19:30:00    
location_b    2400    167    1    9    72.92    i    17-Oct-2011    20:11:00    
location_p    2401    167    2    9.75    72.77    i    18-Oct-2011    00:25:00    
location_p    2401    167    3    10.57    72.75    i    18-Oct-2011    18:24:00    
location_d    2402    167    4    10.53    73    i    18-Oct-2011    19:30:00    
location_d    2402    167    4    10.53    73    i    18-Oct-2011    19:30:00

trying to assign number in last column (9th) according to the change in first column(string change)

Code:
location_z    2399    167    1    9    72.92    i    17-Oct-2011    20:11:00    2448
location_z    2399    167    2    9.75    72.77    i    18-Oct-2011    00:25:00    2448
location_z    2399    167    3    10.57    72.75    i    18-Oct-2011    18:24:00    2448
location_b    2400    167    4    10.53    73    i    18-Oct-2011    19:30:00    2449
location_b    2400    167    1    9    72.92    i    17-Oct-2011    20:11:00    2449
location_p    2401    167    2    9.75    72.77    i    18-Oct-2011    00:25:00    2450
location_p    2401    167    3    10.57    72.75    i    18-Oct-2011    18:24:00    2450
location_d    2402    167    4    10.53    73    i    18-Oct-2011    19:30:00    2451
location_d    2402    167    4    10.53    73    i    18-Oct-2011    19:30:00    2451

in another file location is not available only numeric value here I want to consider 1st column numeric value to assign number

Code:
2399    167    1    9    72.92    i    17-Oct-2011    20:11:00    
2399    167    2    9.75    72.77    i    18-Oct-2011    00:25:00    
2399    167    3    10.57    72.75    i    18-Oct-2011    18:24:00    
2400    167    4    10.53    73    i    18-Oct-2011    19:30:00    
2400    167    1    9    72.92    i    17-Oct-2011    20:11:00    
2401    167    2    9.75    72.77    i    18-Oct-2011    00:25:00    
2401    167    3    10.57    72.75    i    18-Oct-2011    18:24:00    
2402    167    4    10.53    73    i    18-Oct-2011    19:30:00    
2402    167    4    10.53    73    i    18-Oct-2011    19:30:00

expected output

Code:
2399    167    1    9    72.92    i    17-Oct-2011    20:11:00    2448
2399    167    2    9.75    72.77    i    18-Oct-2011    00:25:00    2448
2399    167    3    10.57    72.75    i    18-Oct-2011    18:24:00    2448
2400    167    4    10.53    73    i    18-Oct-2011    19:30:00    2449
2400    167    1    9    72.92    i    17-Oct-2011    20:11:00    2449
2401    167    2    9.75    72.77    i    18-Oct-2011    00:25:00    2450
2401    167    3    10.57    72.75    i    18-Oct-2011    18:24:00    2450
2402    167    4    10.53    73    i    18-Oct-2011    19:30:00    2451
2402    167    4    10.53    73    i    18-Oct-2011    19:30:00    2451

# 2  
Old 03-04-2013
Try:
Code:
awk '{print $0, $col + val}' col=2 val=49 file

# 3  
Old 03-04-2013
suppose if I have to assign number from 1 to n then ?

location_a is 1
location_b is 2

2399 is 1
2400 is 2

if case is like this then ?
# 4  
Old 03-04-2013
You mean on the basis change alone? You could try this ( assuming entries are grouped by $1 ):
Code:
awk 'p!=$1x{n++; p=$1}{print $0, n-1}' n=2451 file

Starting from 1:
Code:
awk 'p!=$1x{n++; p=$1}{print $0, n-1}' n=1 file


Last edited by Scrutinizer; 03-04-2013 at 03:46 AM..
# 5  
Old 03-04-2013
Thank you so much...
#4 code is working for both file ....will you please explain above code..
# 6  
Old 03-04-2013
Sure:

Code:
awk '
  p!=$1x{           # if previous string value for first field ($1x: force string context by concatenation of $1 and the empty variable "x") is unequal to the current first field
    n++             # then increase the number
    p=$1            # set p to the current field
  }
  {
    print $0, n-1   # print the current line followed by the number - 1
  }
' n=2451 file       # pass the value to n and initialize it at 2451


Last edited by Scrutinizer; 03-04-2013 at 03:50 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 03-04-2013
suppose if I have to repeat values say for example input file is like this
Code:
0
1
2
3
5
7
9
10
2
3
5
3
5
6

output requirement is as follows

Code:
col1 output
0 1
1 2
2 3
3 4
5 5
7 6
9 7
10 8
2 1
3 2
5 3
3 1
5 2
6 3

what I need to do for awk script ?

---------- Post updated at 02:32 AM ---------- Previous update was at 02:29 AM ----------

Thank you so much for useful post(#6)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign number of records to a variable

How does one assign a variable, x to equal the number of records in a different file. I have a simple command such as below: awk -F "\t" '(NR>5) { if(($x == "0/0")) { print $0} }' a.txt > a1.txt but I want x to equal the number of records in a different file, b.txt (10 Replies)
Discussion started by: Geneanalyst
10 Replies

2. Shell Programming and Scripting

Assign large number of blanks to a variable

I want to assign large number of blanks to a variable in Korn shell. If it is a small number it is fine like if I want to assign 3 blanks I would code var=" " But if it is a big number say 100 blanks, what is a better way? Ultimately I will use it in printf statement printf... (3 Replies)
Discussion started by: Soham
3 Replies

3. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

4. Shell Programming and Scripting

Assign a variable to number of lines in a file

Hello Gurus, Here is my requirement. I need to find the number of lines in a file and need to assign it to a variable. This is what I did and not wroking. #!/bin/ksh set -xv Src_Path=/mac/dev/Generic/SrcFiles Src_Count=wc -l ${Src_Path}/FILE_JUNE.txt Count_file = $Src_Count | awk -F... (2 Replies)
Discussion started by: thummi9090
2 Replies

5. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

6. Shell Programming and Scripting

Arithmetic (number-based) if condition

Hi Folks I'm looking for help with if statement. I'm reading the file with header (starts with 0 on position 1 in the line) and data (starts with 1 on position 1 in the line). I have to check if the number from header (should be number of data rows) equal actual count of the data rows. ... (4 Replies)
Discussion started by: viallos
4 Replies

7. Shell Programming and Scripting

How to assign a shell variable to a NUMBER parameter in pl/sql?

I have the below script running for generating file from PL/SQL stored procedure. I need to declare a shell variable and then pass this to sqlplus command to pass the same as a INPUT parameter for the stored procedure. Please help to do this. #!/bin/sh minlimit=0 maxlimit=10 size=100 while... (0 Replies)
Discussion started by: vel4ever
0 Replies

8. Shell Programming and Scripting

Get letter from number and assign to a variable

Hi to all in forum, I'm trying to convert the letter number between 1 (A) and 26 (Z), that part is working, my issue is how to assign the printf output to a variable:LetterNumber=10 printf "\x$(printf %x $((${LetterNumber}+64)))" $ J #The problem, how to assign printf output (J in this... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

9. Shell Programming and Scripting

Count the number of lines in a file with one condition

Hi Everyone, 1.txt Mon 08 Feb 2010 12:30:44 AM MYT;1265560244;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:46 AM MYT;1265560246;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:48 AM MYT;1265560248;e164:0000116047275464;T;Central;0; Mon 08 Feb 2010 12:30:50 AM... (1 Reply)
Discussion started by: jimmy_y
1 Replies

10. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies
Login or Register to Ask a Question