The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Cut display multiple lines of output cbo0485 Shell Programming and Scripting 2 08-31-2009 04:08 PM
How do I capture multiple lines of the status output of a command? zzz1528 Shell Programming and Scripting 2 06-19-2009 12:48 PM
sed find and replace multiple lines supersimha Shell Programming and Scripting 6 04-04-2009 03:35 AM
using tr to put multiple lines of output into one line otes4 Shell Programming and Scripting 3 02-18-2008 11:30 AM
Display multiple output lines kingofprussia UNIX for Dummies Questions & Answers 5 05-23-2007 08:56 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
wilg0005 wilg0005 is offline
Registered User
  
 

Join Date: Nov 2009
Location: Jacksonville, FL
Posts: 3
Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For example, I'd like to find these:
Code:
 
MOVE 'LIT1'  TO VAR1
MOVE 'LIT2'  TO VAR2
...and print this:
Code:
 
LIT1LIT2
This could happen multiple times in one file. For example, if these were the entries:
Code:
 
some junk
MOVE 'LIT1'  TO VAR1
other junk
MOVE 'LIT2'  TO VAR2
more junk
more junk
MOVE 'LIT3'  TO VAR1
MOVE 'LIT4'  TO VAR2
junk again
MOVE 'LIT5'  TO VAR1
more junk
more junk
MOVE 'LIT6'  TO VAR2
junk
I'd like to print this:
Code:
 
LIT1LIT2
LIT3LIT4
LIT5LIT6
  #2 (permalink)  
Old 2 Weeks Ago
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
What tells you that only two MOVE statments get combined?

Code:
MOVE 'LIT1' TO VAR1.
Do all lines end with a period? Probably not.
Try this to start with
Code:
egrep '(VAR1$|VAR1\.$|VAR2$|VAR2\.$)'  mycobol.cob | 
       awk '{ printf("%s", $2); if(FNR%2==0) {print}  }'  | tr -d \'
  #3 (permalink)  
Old 2 Weeks Ago
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
Code:
cat /your/file | sed -rn "/MOVE '[^']*'  TO VAR[12]/p" | sed -r "/1$/N; s/MOVE '([^']*)'  TO VAR1\nMOVE '([^']*)'  TO VAR2/\1\2/"
hope this helps
  #4 (permalink)  
Old 2 Weeks Ago
wilg0005 wilg0005 is offline
Registered User
  
 

Join Date: Nov 2009
Location: Jacksonville, FL
Posts: 3
Thumbs up close...

Hey Jim,

Thanks, that is really close; I'm getting the output below. How can I print a new line instead of the second grep output (i.e. 'MOVE LIT2 TO VAR2') and a new line?

Code:
LIT1LIT2MOVE LIT2  TO VAR2
LIT3LIT4MOVE LIT4  TO VAR2
LIT5LIT6MOVE LIT6  TO VAR2
-Jay

Hey chebarbudo,

My grep doesn't like the -r option; I get 'sed: illegal option -- r'.

---------- Post updated at 05:27 PM ---------- Previous update was at 05:24 PM ----------

Jim,

Also, I forgot to answer..

There are always 2 move statements with the same variable names as a coding convention our programmers use.

Right, there will not always be a period at the end of the statement.

-Jay

---------- Post updated at 05:33 PM ---------- Previous update was at 05:27 PM ----------

Thanks Jim, I got it to work by using this:

Code:
egrep '(VAR1$|VAR1\.$|VAR2$|VAR2\.$)'  $program | 
       awk '{ printf("%s", $2);  if(FNR%2==0) {printf("\n")}}'  | tr -d \'
 
LIT1LIT2
LIT3LIT4
LIT5LIT6
Thanks again!

-Jay
  #5 (permalink)  
Old 2 Weeks Ago
rdcwayx rdcwayx is online now
Registered User
  
 

Join Date: Jun 2006
Posts: 210
Quote:
Originally Posted by wilg0005 View Post
egrep '(VAR1$|VAR1\.$|VAR2$|VAR2\.$)' $program |
awk '{ printf("%s", $2); if(FNR%2==0) {printf("\n")}}' | tr -d \'
With -F, you needn't use tr command.

Code:
awk -F[\'\ ] '{ printf("%s", $3);  if(FNR%2==0) {printf("\n")}}'
  #6 (permalink)  
Old 2 Weeks Ago
wilg0005 wilg0005 is offline
Registered User
  
 

Join Date: Nov 2009
Location: Jacksonville, FL
Posts: 3
Thumbs up

Hey chebarbudo,

Yes, that worked as well.

Code:
 
cat /myfiles/test.cob | sed -n "/MOVE '[^']*'  TO VAR[12]/p" | sed "/1$/N; s/MOVE '\([^']*\)'  TO VAR1\nMOVE '\([^']*\)'  TO VAR2/\1\2/"

LIT1LIT2
LIT3LIT4
LIT5LIT6
Also, thanks for the tip rdcwayx.
  #7 (permalink)  
Old 2 Weeks Ago
chebarbudo's Avatar
chebarbudo chebarbudo is offline
Registered User
  
 

Join Date: Nov 2008
Location: various
Posts: 188
Hey Jay,

I'm glad you already found the answer. Can you anyway tell me if this new syntax works?
Code:
cat /your/file | sed -n "/MOVE '[^']*'  TO VAR[12]/p" | sed "/1$/N; s/MOVE '\([^']*\)'  TO VAR1\nMOVE '\([^']*\)'  TO VAR2/\1\2/"
Reply

Bookmarks

Tags
awk, concatenate, multiple, pattern, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0