suggestion on awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting suggestion on awk
# 1  
Old 04-21-2010
suggestion on awk

I have a file something like below

Code:
RMR*TN*11111111*....
REF*TN*
RMR*AB*2222222*...
SE*000010*...
ST*820*......
BPR*C*....
TRN*1*....
REF*TN*....
RMR*TN*11111111*...
REF*CD*...
RMR*EF*4444444*...
SE*000010*
GE*2*

I need to fetch only the loop (Starts with RMR and ends with REF) which matches a particular account number (In my case, we will consider 11111111). I am using the awk command to fetch the records based on the account number.

Code:
awk ' /'"$acct"'/&& /RMR/{print;flag=1;next} (/RMR/ || /^ST/) {flag=0} flag { print }' FILENAME > OUTPUT

Before that I need to have the line numbers for the records starting with RMR. With the above script, I get a result something like

Code:
10:RMR*TN*11111111*....
REF*TN*
SE*000010*...
20:RMR*TN*11111111*...
REF*CD*...
SE*000010*
GE*2*

After this I wanted to have only the RMR loop. I am trying something like this,

Code:
awk ' /'"$acct"'/&& /RMR/{print;flag=1;next} /RMR/{flag=0} flag { print }'

to get something like
Code:
10:RMR*TN*11111111*....
REF*TN*
20:RMR*TN*11111111*...
REF*CD*...

Can anyone advise.
# 2  
Old 04-21-2010
Try this:

Code:
awk -v acc=$acct '$0 ~ /^RMR/ && $0 ~ acc{print NR,":",$0;getline;while($0 !~ /^REF/){print;getline}print;next}' filename


cheers,
Devaraj Takhellambam
# 3  
Old 04-22-2010
It prints the corrrect value and goes infinite..

The RMR loop can be something like

Code:
Only RMR*.....
or
RMR*...
REF*....
or
RMR*....
REF*....
DTM*...

Sorry for missing this

---------- Post updated 04-22-10 at 05:29 AM ---------- Previous update was 04-21-10 at 07:42 AM ----------

I am able to get the values with the below code when I search for an account "11111111"

With the following as the input
Code:
ST*.....
BPR*A*.... 
REF*.....
RMR*TN*11111111*.... 
REF*TN* 
RMR*AB*2222222*... 
SE*000010*... 
ST*820*...... 
BPR*C*.... 
TRN*1*.... 
REF*TN*.... 
RMR*TN*11111111*...
REF*CD*... 
RMR*EF*4444444*... 
SE*000010*
 GE*2*

and this as the code

Code:
STList=`grep -c  "^ST.*" INPUTFILE  #Basically checking for how many ST-SE sections
 
awk ' /'"$acct"'/&& /RMR/{print;flag=1;next} (/RMR/ || /^ST/) {flag=0} flag { print }'  FILENAME > OUTPUT #separating the RMR with the same account number
 
if [ $STList -gt 1 ] ;then
            awk ' /'"$acct"'/&& /RMR/{print;flag=1;next} (/RMR/ || /^SE/) {flag=0} flag { print }' $OUTPUT  > $tmp/tmpOutput
    fi

I am able to get the below as the output
Code:
RMR*TN*11111111*.... 
REF*TN* 
RMR*TN*11111111*...
REF*CD*...

Can i get the remaining rows simultaneously in a different files. Basically the 1st part till RMR and the 2nd part till the next RMR and so on.. and finally the last part.

Can anyone advise
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Regarding suggestion

Hello Admin/moderators Team, Greetings !! This is regarding a suggestion which may help our forum if this is possible. Thanks, R. Singh "GOD helps those who help themselves" (2 Replies)
Discussion started by: RavinderSingh13
2 Replies

2. Post Here to Contact Site Administrators and Moderators

Suggestion: adding two new groups "sed" and "awk"

Majority of the questions are pertaining file/string parsing w.r.t sed or awk It would be nice to have these two as their own sub category under shell-programming-scripting which can avoid lot of duplicate posts. (1 Reply)
Discussion started by: jville
1 Replies

3. Shell Programming and Scripting

Need Suggestion

Hi, I have the requirement to run multiple SQLs against database running on a server and save the output of SQL executed to a log. I thought about writing a Function in sh that will take file name as argument and function should return the SQL output to calling program. E.g. function Run_SQL... (0 Replies)
Discussion started by: bhupinder08
0 Replies

4. UNIX for Dummies Questions & Answers

OS suggestion

Hello, I'm working on a Linux 2.6.32-33-server (Ubuntu 4.4.3). I typed in man -k package and got e.g. apt I typed in apt --help and got: The program 'apt' is currently not installed. You can install it by typing: sudo apt-get install openjdk-6-jdkI don't understand where this... (2 Replies)
Discussion started by: daWonderer
2 Replies

5. Shell Programming and Scripting

Replace line and field using SED and/or AWK or some other suggestion

QUESTION 1: How do you replace a specific line (i.e. line 4) with a new user defined line (i.e. the contents of SAMS’s name, history, math and English grades have been set already). I have been attempting to use SED (FYI: I don’t have GNU SED) or AWK, but haven’t had any luck. FYI: I am using... (1 Reply)
Discussion started by: thibodc
1 Replies

6. Post Here to Contact Site Administrators and Moderators

Suggestion

Can we have a forum that is dedicated to topics concerning power management ? It'd be great to have a few moderators to address these issues concerning the power-management. More-so-ever since research related to power management at the OS level is kicking up a lot of dust and becoming a hot area... (7 Replies)
Discussion started by: vishwamitra
7 Replies

7. Shell Programming and Scripting

need your suggestion

Hi all: I need your suggestion about how to making this script Purpose:- Monitor log for the system OS: Unix Sun Solaris 10 Hold oracle database 10 g Life time for the system cycle to 48 hours the system working as the follow 1- the system is divided into three steps 2-... (0 Replies)
Discussion started by: dellsh
0 Replies

8. Shell Programming and Scripting

Need your suggestion please..

can anyone rite here guide me. i want to know which reference books that all of you recommended for C Shell dummies like me...(beginner) (1 Reply)
Discussion started by: unknown2205
1 Replies

9. Programming

I want a suggestion

I am a student and I love the computer very much , especially in programming. However I know little about programming . I don't know which I should learn (JAVA and C++). Would you like to give me a suggestion ? Thanks! (6 Replies)
Discussion started by: camel
6 Replies

10. Post Here to Contact Site Administrators and Moderators

Suggestion

As I was just pondering to myself I relized that emergency's do happen and there are times when people need an anwser to their problem ASAP. So what i was thinking why not add a live chat to this board this way people could chat amongst each other in real time. I know this could be done with a... (1 Reply)
Discussion started by: tovsky
1 Replies
Login or Register to Ask a Question