awk incremental replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk incremental replace
# 1  
Old 02-23-2011
awk incremental replace

Hello!

I have the following lines in a file:

Code:
 
1|0|HEADER|
2|1|HEADER|
3|1|MAIN|
4|1|INFO|
5|1|INFO|
6|1|INFO|
7|2|INFO|
8|4|INFO|
9|55|FOOTER|
10|1|HEADER|
11|22|MAIN|
12|9|MAIN|

and I want to convert it into:

Code:
 
1|0|HEADER|
2|1|HEADER|
3|1|MAIN|
4|1|INFO|
5|1|INFO|
6|1|INFO|
7|1|INFO|
8|1|INFO|
9|1|FOOTER|
10|2|HEADER|
11|2|MAIN|
12|2|MAIN|

That is start second field with 0 populate next line's second field with that number and every time you find "HEADER" in the 3rd field increment that number.

This is what I managed so far:

Code:
awk 'BEGIN{OFS=FS="|"}{i=0;if($3=="HEADER"){i+=i}}$2{$2=i}{print > "temp"}' file

However it gives me the wrong result, it does increment the line where it finds "HEADER" but the next line it populates it with 0 again:

Code:
 
1|1|HEADER|
2|1|HEADER|
3|0|MAIN|
4|0|INFO|
5|0|INFO|
6|0|INFO|
7|0|INFO|
8|0|INFO|
9|0|FOOTER|
10|1|HEADER|
11|0|MAIN|
12|0|MAIN|

Any ideas would be much apreciated Smilie
# 2  
Old 02-23-2011
upto my knowledge i have a solution.
remove the first field and sort the data according the second field.
after that add numbers.

regards
rajesh
# 3  
Old 02-23-2011
Quote:
Originally Posted by rajesh_pola
upto my knowledge i have a solution.
remove the first field and sort the data according the second field.
after that add numbers.

regards
rajesh
Tha did not work, it jsut replaced all the numbers to 0 Smilie
I don't just need to sort the second field, I need to number it incrementing by 1 each time in the 3rd field there is a "HEADER"

so
Code:
1|0|MAIN
2|0|HEADER
3|0|INFO
4|0|FOOTER
5|0|HEADER
6|0|MAIN

would be:
Code:
1|0|MAIN
2|1|HEADER
3|1|INFO
4|1|FOOTER
5|2|HEADER
6|2|MAIN

regards

Last edited by Franklin52; 02-24-2011 at 03:21 AM.. Reason: Please use code tags
# 4  
Old 02-23-2011
Does this help

Code:
 
awk -v c=0 -F"|" '/HEADER/ { $2=c;print ; prev=c ; c=c+1 ;next} {$2=prev;print}' OFS="|" input_file

This User Gave Thanks to panyam For This Post:
# 5  
Old 02-23-2011
Code:
nawk '$3=="HEADER" {$2=c++}1' FS='|' OFS='|' myFile

# 6  
Old 02-23-2011
Quote:
Originally Posted by vgersh99
Code:
nawk '$3=="HEADER" {$2=c++}1' FS='|' OFS='|' myFile

Hi vgersh99,

Your code does not work as the OP expected.
# 7  
Old 02-23-2011
Quote:
Originally Posted by panyam
Hi vgersh99,

Your code does not work as the OP expected.
true, but the OP's description and the sample input/output contradict each other as well.
Given the 'description', here's the modified code:
Code:
nawk '$3=="HEADER" {$2=c++}$2=c' FS='|' OFS='|' myFile

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. UNIX for Beginners Questions & Answers

Search and incremental replace

Dears, Can you help me with the below concern, I need one liner for this. Input_file.txt dn: asdf=yyyyyyyyyyyyy426019001711473,ou=multiSCs,dc=btc asdf: yyyyyyyyyyyyy426019001711473 DSUnitGroup: 1 objectClass: CUDBMultiServiceConsumer dn:... (1 Reply)
Discussion started by: Kamesh G
1 Replies

3. UNIX for Beginners Questions & Answers

Incremental logic

Hi Guys, am trying to write logic to do incremental value using linux Example: a=00.00.00.01 My b should be like this b=00.00.00.02 and when it reaches 99 my b should look like this b=00.00.01.99 Appreciate your help guys Please use CODE tags when displaying sample input, sample... (14 Replies)
Discussion started by: buddi
14 Replies

4. Shell Programming and Scripting

FULL or INCREMENTAL ???

Hi. Can someone tell me if the following script that i have made is a script for INCREMENTAL BACKUP or FULL BACKUP. My teacher told me that is doing an FULL BACKUP. • find /etc /var /home -newer /backups/.backup_reference > /backups/.files_to_archive • touch /backups/.backup_reference • tar... (1 Reply)
Discussion started by: bender-alex
1 Replies

5. Shell Programming and Scripting

Using awk to append incremental numbers to the end of duplicate file names.

I'm currently working on a script that extracts files from a .zip, runs an sha1sum against them and then uses awk to pre-format them into zomething more readable thusly: Z 69 89e013b0d8aa2f9a79fcec4f2d71c6a469222c07 File1 Z 69 6c3aea28ce22b495e68e022a1578204a9de908ed File2 Z 69... (5 Replies)
Discussion started by: Ethereal
5 Replies

6. UNIX for Dummies Questions & Answers

incremental by 1

let says, i have this number as 000002080, i want to add 1 to make it 000002081, and then i want to add 1 to 000002082, add 1 to 000002083, 84. i=000002080 TOT=$(echo "scale=9; $i + 1" | bc) echo $TOT it shows 2081, i want to retain 000002081, 000002082, 000002082, 000002084. (2 Replies)
Discussion started by: tjmannonline
2 Replies

7. Shell Programming and Scripting

Incremental backup

Hi, I would like to create a daily incremental backup of a directory with all of the files within and add a timestamp (year-month-day) to the tar.gz file. I have the following, but it doesn't backup the inside files of the directory. #!/bin/bash tar -czf... (1 Reply)
Discussion started by: agasamapetilon
1 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

find & incremental replace?

Looking for a way using sed/awk/perl to replace port numbers in a file with an incrementing number. The original file looks like... Host cmg-iqdrw3p4 LocalForward *:9043 localhost:9043 Host cmg-iqdro3p3a LocalForward *:10000 localhost:10000 Host cmg-iqdro3p3b LocalForward... (2 Replies)
Discussion started by: treadwm
2 Replies

10. UNIX for Dummies Questions & Answers

incremental backup

Hi All.. i am trying to write a script which will give the incremental tar backup of all files with latest timestam. i tried with find -mmin -2 but if it takes half on hour or something to creat the tar itself, then no meaning in using the above command. so please help me to find the... (2 Replies)
Discussion started by: Usha Shastri
2 Replies
Login or Register to Ask a Question