Increment a column using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increment a column using awk
# 1  
Old 07-07-2009
Increment a column using awk

Hi,

I have a sample file like below:

213~!0~!Feb 16 2009 4:57:29:833PM~!0
212~!0~!Feb 7 2009 5:29:57:760PM~!0
211~!0~!Feb 4 2009 5:51:40:863PM~!0
209~!0~!Dec 17 2008 3:19:05:043PM~!0
206~!0~!Dec 4 2007 4:01:02:850PM~!0

"~!" is the field seperator.

I need to replace the first row, first column of the file with a variable passed to it and then increment the first column of the subsequent rows by 1. For eg, the variable is 500. the output should be.

500~!0~!Feb 16 2009 4:57:29:833PM~!0
501~!0~!Feb 7 2009 5:29:57:760PM~!0
502~!0~!Feb 4 2009 5:51:40:863PM~!0
503~!0~!Dec 17 2008 3:19:05:043PM~!0
504~!0~!Dec 4 2007 4:01:02:850PM~!0

Any help on this is much appreciated.

Thanks
Hemant
# 2  
Old 07-07-2009
Try this,

cat <Filename> | sed 's/\~\!/\-/g' | awk -F'-' 'BEGIN{t=500} {print t"~\!"$2"~\!"$3"~\!"$4; t+=1}'
# 3  
Old 07-07-2009
Increment a column using awk

Hi Ramesh,

Thanks for this, it works perfect.

Just in case if this has more columns so that it is not very easy to print to print each column separatley, {print t"~!"$2"~\!"$3"~\!"$4; t+=1}. Is there a way of doing it with something like a { print $0 }, may not exactly this but something better.

Thanks
Hemant
# 4  
Old 07-07-2009
Try this,

cat <Filename> | sed 's/\~\!/\-/g' | awk -F'-' 'BEGIN{t=500} { printf t; for(i=2; i<=NF; i++) printf "~\!"$i; printf "\n"; t+=1; }'

-srk
# 5  
Old 07-07-2009
Thank you

Thanks Ramesh, it works.
# 6  
Old 07-07-2009
Code:
nawk -v number=500 'BEGIN{FS=OFS="~!"} {$1=number;print;number++;}' yourfile

perl:
Code:
my $num=500;
while(<DATA>){
	s/^[0-9]+/$num++/e;
	print;
}
__DATA__
213~!0~!Feb 16 2009 4:57:29:833PM~!0
212~!0~!Feb 7 2009 5:29:57:760PM~!0
211~!0~!Feb 4 2009 5:51:40:863PM~!0
209~!0~!Dec 17 2008 3:19:05:043PM~!0
206~!0~!Dec 4 2007 4:01:02:850PM~!0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Increment with awk - how to define start value

Hello, I am running under ubuntu18.04 My question is about awk. inputfile 0wo010011oasasds sdjhsdjh=, u12812888 8jsjkahsjajnsanakn akjskjskj=, suhuhuhwx kskkxmsnnxsnjxsnjxsnjjnjjdi=, 22878ssssss Below code adds consecutive numbers when string = is found run_code: awk -F'=' -v OFS='='... (4 Replies)
Discussion started by: baris35
4 Replies

2. Shell Programming and Scripting

Check increment values in column

Gents, I have a file where i would like to check the constant increment by 2 in column 2. 5450 1000 5450 1002 5450 1004 5450 1006 5465 1000 5465 1002 5465 1006 5465 1008 5550 1002 5550 1004 5550 1006 5550 1008 6830 1000 6830 1002 6830 1008 6830 1010 (6 Replies)
Discussion started by: jiam912
6 Replies

3. Shell Programming and Scripting

Increment Column having Alphanumeric value in file

I want to replace a column(first and last) having an alphanumeric value in a file. Requirement : 1)All values in a Column must be unique and contain an incremented pattern “HCTV0096” for first column and “cafefeca0090” for last column 2)for uniquely identifying each value in column Numeric part... (6 Replies)
Discussion started by: ketanraut
6 Replies

4. Shell Programming and Scripting

Problems with awk (fatal error) and paste (two variables into one column-by-column)

Hello, I have a script extracting columns of useful numbers from a data file, and manipulating the numbers with awk commands. I have problems with my script... 1. There are two lines assigning numbers to $BaseForAveraging. If I use the commented line (the first one) and let the second one... (9 Replies)
Discussion started by: vgbraymond
9 Replies

5. Shell Programming and Scripting

Create new file with increment column based on conditions

Hello, Using bash script, i need to process the following file: 887,86,,2013-11-06,1,10030,5,2,0,200,, 887,86,,2013-11-05,1,10030,5,2,0,199,, 887,138,,2013-11-06,1,10031,6,2,0,1610612736,, 887,164,,2013-11-06,1,10000,0,2,0,36000,, and to create a new file such as the below ... (2 Replies)
Discussion started by: JonhyDeep
2 Replies

6. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

Increment existing column in file

Hi All, I have a file with 3 millions records in which 3rd column is same throughout say its value is 0 throughout.for example: Col1 Col2 Col3 Col4 A 1 0 5 B 2 0 6 C 3 0 7 D 4 0 9 I want my output as : Col1 Col2 Col3 Col4 A 1 ... (4 Replies)
Discussion started by: Pinky456
4 Replies

8. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
3 Replies

9. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

10. Shell Programming and Scripting

Awk - Compare fields and increment variables

Hi, My first post to this group... I have a need to to parse a source file which is a capture from a network analyser. I have two fields that need to be checked: - Field 7 represents the packet length (an integer), and Field 4 represents a network address (e.g. 192.168.25.3) - The... (10 Replies)
Discussion started by: mv652
10 Replies
Login or Register to Ask a Question