AWK script to count and then replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK script to count and then replace
# 1  
Old 11-11-2008
AWK script to count and then replace

Hi All,

I am looking for a simple awk script which can count the segment delimeters of a file and then replace the count in the UNT segment.

For example i am showing the below instance of a file:-

UNA:+.?*'
UNB+UNOB:4+0050:ZZZ+DBH+20081105:1803+0811051803'
UNG+IFTMIN+0050+3995+20081105:1803+0811051803+UN+D:99ASmilieEBL02'
UNH+0811051803+IFTMINSmilie:99A:UNSmilieEBL02'
BGM+705+C00005738+5'
CTA+MS+:EnterpriseBatchProcessor'
DTM+137:200811031729:203'
FTX+AEA++9'
CNT+11:22:PCE'
CNT+7:22500:KGM'
CNT+15:0.053:MTQ'
DOC+710++EI+2+0'
LOC+57+DEHAM:::Hamburg'
RFF+FF:C00005738'
TDT+20+125+1+13:OCEAN VESSEL+00009999:172+++:::COSCO LONG BEACH'
DTM+133:20081105:102'
LOC+9+DEHAM:::Hamburg'
LOC+12+SGSIN:::Singapore'
NAD+CN+00004225+11 JOO KOON CIRCLE:JURONG:11 JOO KOON CIRCLE:SINGAPORE+ALFA LAVAL SINGAPORE PTE LTD++++629043'
NAD+CZ+00007306+ALTENAER STRASSE 72-76Smilie-58675 HEMER:ALTENAER STRASSE 72-76Smilie-58675 HEMER:HH+MATRIX VERTRIEBSSERVICE GMBH'
CTA+IC+:EnterpriseBatchProcessor'
NAD+N1+00012726+Amselstrasse::Amselstrasse:Hamburg:HH+Hanjin Shipping Co. Ltd. Schiffsmak++++20457'
GID+1+22:PA:::PACKET'
FTX+AAA+++MACHINERY PARTS'
MEA+WT+AAE+KGM:22500'
MEA+VOL+AAE+MTQ:0.053'
RFF+BN:HJSHAM123456'
PCI++ALFA LAVAL SINGAPORE'
SGP+HJCU8521230+22'
EQD+CN+HJCU8521230+42G0'
TMD+3:FCL/FCL+FCL'
SEL+123963+SH'
RFF+BN:SDEHAG0000023'
UNT+31+0811051803'
UNE+1+0811051803'
UNZ+1+0811051803'

We need to count the number of segment delimeters i.e. the symbol ' from the UNH to the UNT segment and then replace the UNT+31 the 31 value with the exact count.

Please help. Any other information i will let you know.

Regards
Karan
# 2  
Old 11-11-2008
I have created the below one:--

awk '{sub(/^UNT\+[0-9]+/,"UNT+" c);print};/^UNH/,/^UNT/{if( index($0,"'"'"'") ){c++}}'

But it is not giving me the correct output.

Can someone please shed some light on this.

Thanks

Karan
# 3  
Old 11-11-2008
Try:

Code:
awk '/^UNH/,/^UNT/ { c++; sub(/^UNT\+[0-9]+/,"UNT+" c); print; }' < file

# 4  
Old 11-11-2008
It did not help...no output came while running the command on the input file
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 or sed script to count number of occurrences and creating an average

Hi Friends , I am having one problem as stated file . Having an input CSV file as shown in the code U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0... (4 Replies)
Discussion started by: kshitij
4 Replies

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

3. Shell Programming and Scripting

awk script to detect specific string in a log file and count it

Hello, can someone guide me on this? I don't know what is the best approach, (awk script, shell script) I am using RedHat Linux version 6.5. There is a third party application deployed on that server. This app by default generates 5 log files and each file is 20MB. These log rollover... (5 Replies)
Discussion started by: ktisbest
5 Replies

4. Shell Programming and Scripting

Trying to get an awk script to replace values in column

I'm trying to make an awk script to compare values I've set as var1, var2, and var3 earlier in the script to the values in the userinputted column of four text files called Node1.txt, Node2.txt, Node3.txt, and Node4.txt and then replace the values in that userinputted column with either ttt or gcc,... (8 Replies)
Discussion started by: Eric1
8 Replies

5. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

6. Shell Programming and Scripting

awk: replace with script parameter

Hi var=0001 I want to replace 2nd field of file with variable var in file sample.txt Please suggest with awk. dont want to use awk -v option. pseudo code : something like this. var=0001 awk '{ 12193 /var } {print $0 }' sample.txt (2 Replies)
Discussion started by: theshashi
2 Replies

7. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

8. Shell Programming and Scripting

Help in writing a find/replace script using awk

Hi All, I need to write a script that will go through 600+ files and perform find and replace. I was going to use sed but there is a level of complexity that is doing my head in. To explain: I have 600+ files that have a line in them that reads (for example) FILE=DCLCLHST... (4 Replies)
Discussion started by: Kocko
4 Replies

9. Shell Programming and Scripting

awk script to count percentage from log file

Hi, I have a log like this : actually i want to get the log like this : where % can get from : 100 * pmTotNoRrcConnectReqSucc / pmTotNoRrcConnectReq Thanks in advance.. :) (8 Replies)
Discussion started by: justbow
8 Replies

10. Shell Programming and Scripting

AWK Script - Count Files In Directories

Hey, I'm very new to AWK and am trying to write a script that counts the number of files in all subdirectories. So, basically, my root has many subdirectories, and each subdirectory has many files. How can I get the total count? I haven't been able to figure out how to loop through the... (1 Reply)
Discussion started by: beefeater267
1 Replies
Login or Register to Ask a Question