![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
|
|
||||
|
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 Code:
LIT1LIT2 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 Code:
LIT1LIT2 LIT3LIT4 LIT5LIT6 |
|
||||
|
What tells you that only two MOVE statments get combined?
Code:
MOVE 'LIT1' TO VAR1. Try this to start with Code:
egrep '(VAR1$|VAR1\.$|VAR2$|VAR2\.$)' mycobol.cob |
awk '{ printf("%s", $2); if(FNR%2==0) {print} }' | tr -d \'
|
|
||||
|
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 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
-Jay |
|
||||
|
Quote:
Code:
awk -F[\'\ ] '{ printf("%s", $3); if(FNR%2==0) {printf("\n")}}'
|
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| awk, concatenate, multiple, pattern, sed |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|