Help with awk script to append seq num at end of record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk script to append seq num at end of record
# 1  
Old 09-27-2016
Help with awk script to append seq num at end of record

Hi Unix forum.

I have the following requirement to add a sequence value to each record in a file but only if it meets certain conditions. Field value in pos. 1 and 2 must be '0B' or 'OA' else leave as is. Sequence value must be preserved for each OB and OA pair.

Code:
Data Before:
123
456
OB123
OA456
OB789
OA890
999

Data After:
123
456
OB123 1
OA456 1
OB789 2
OA890 2
999

Code:
This is what I have so far but it's not working 100% as expected.  It's adding the sequence value to the last record also.

awk '{var0=substr($0,1,2)} var0=="AB" {COUNTER = COUNTER+1} {$0 = $0 FS COUNTER} {var1=substr($0,1,2)} var1=="AO" {$0 = $0 FS COUNTER} 1'

Can someone help me refine my code?

Thank you.
# 2  
Old 09-27-2016
Hi,

Can you try this ?

Code:
awk 'BEGIN {i=1;j=1} {a=substr($0,1,2);if (a == "OA") { print $0,i++;} else if (a == "OB") print $0,j++; else print $0 } ' file

Code:
cat file
123
456
OB123
OA456
OB789
OA890
999
OA346
OB709

output :
Code:
123
456
OB123 1
OA456 1
OB789 2
OA890 2
999
OA346 3
OB709 3

Non-awk :
Code:
 i=1;j=1;while read line; do if [[ "$line" == OA* ]]; then echo $line $(( i++ )); elif [[ "$line" == OB* ]]; then echo $line $((j++)); else echo $line; fi; done < file


Last edited by greet_sed; 09-27-2016 at 06:18 PM.. Reason: Add other solution
# 3  
Old 09-27-2016
Hi,
This works fine :
Code:
$ cat 123.txt
123
456
OB123
OA456
OB789
OA890
999
$ awk '/^OA/{print $0,++a }/^OB/{print $0,++b} !/^O[AB]/' 123.txt
123
456
OB123 1
OA456 1
OB789 2
OA890 2
999

# 4  
Old 09-28-2016
Thank you guys - I will try your suggested code.
# 5  
Old 09-28-2016
Hello pchang,

Could you please try following too and let us know if this helps.
Code:
awk '{printf("%s %s\n",$0,$0=$0~/OA/?++A["OA"]:($0~/OB/?++A["OB"]:""))}'   Input_file

Thanks,
R. Singh
# 6  
Old 09-28-2016
Quote:
Originally Posted by RavinderSingh13
Hello pchang,

Could you please try following too and let us know if this helps.
Code:
awk '{printf("%s %s\n",$0,$0=$0~/OA/?++A["OA"]:($0~/OB/?++A["OB"]:""))}'   Input_file

Thanks,
R. Singh
Hi Ravinder.

it looks like this code works too.

Thanks for providing additional code option.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string and append Indicator to end of record in Linux

Hi All, Could you please help me to achieve below solution. I have a FILE1.txt as below.TEXAS CALIFORNIA TEXAS DALLAS CALIFORNIA CALIFORNIA DALLAS DALLAS TEXAS TEXAS DALLAS NEW YORK NEW YORk FILE2.txt as below.TEXAS,TX DALLAS,DA Now I need to compare the string in FILE2.txt... (6 Replies)
Discussion started by: ureddy
6 Replies

2. Shell Programming and Scripting

awk command to find seq lines

I have a below file FILE.cfg JAN_01 VAR1=4 VAR2=SUM VAR3=PRIVATE JAN_10 VAR1=44 VAR2=GUN VAR3=NATUR JAN_20 VAR1=3 VAR2=TQN VAR3=COMMA code: (JAN_10 is argument passed from script) (6 Replies)
Discussion started by: Roozo
6 Replies

3. 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

4. Shell Programming and Scripting

Need a ksh script for adding the space at the end of record in a flat file

Hi, I need a ksh script for the below requirement: i have a Delimited flat file with 200 records delimiter is '|~|' i need a script to insert space at the end if the record is ending with delimiter '|~|' if it didnt end with delimiter it should not append space. Example: ram|~|2|~| ... (16 Replies)
Discussion started by: srikanth_sagi
16 Replies

5. UNIX for Dummies Questions & Answers

awk script printing each record twice

i have a find command piped to an awk script, I'm expecting it printing the matching record one time but it's doing it twice: the following is my code: find directoryname | awk 'BEGIN { FS="/" } /.*\.$/ || /.*\.$/ { printf "%s/%s\n", $(NF-1), $NF } it gave me the correct output, but just... (2 Replies)
Discussion started by: ymc1g11
2 Replies

6. Shell Programming and Scripting

help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks, I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers. I was not able to do that with the help of a script. All I could was: for i in server1 server2 server3 server4 do ssh $i done I know 'sed' could be used to... (5 Replies)
Discussion started by: momin
5 Replies

7. Shell Programming and Scripting

append "awk command" to the end of each line

hi; this is my qqq.mos: l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-' l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-' l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-' ... i want to append;l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r"... (4 Replies)
Discussion started by: gc_sw
4 Replies

8. Shell Programming and Scripting

append a record at the end of a file

Hi all, i have to append a record at the end of the file(a file which is already with some records).how do i do?please help me? is there any way of doing this with "SED" command.i am not sure.plz help me on this. would appreciate your ideas!!!! bye rao. (3 Replies)
Discussion started by: raoscb
3 Replies

9. UNIX for Dummies Questions & Answers

How to append a semcolon to every record in a file?

Hi, If a file is like 123 456 894 568 234 and the desired o/p is "123" "456" "894" -- -- How can it be done? (2 Replies)
Discussion started by: er_ashu
2 Replies
Login or Register to Ask a Question