Visit Our UNIX and Linux User Community


Find multiple patterns on multiple lines and concatenate output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find multiple patterns on multiple lines and concatenate output
# 1  
Old 11-03-2009
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  
Old 11-03-2009
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  
Old 11-03-2009
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  
Old 11-03-2009
MySQL 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  
Old 11-03-2009
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/"

# 6  
Old 11-03-2009
Quote:
Originally Posted by wilg0005
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")}}'

# 7  
Old 11-03-2009
MySQL

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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #687
Difficulty: Medium
In comparison testing of reverse Polish notation with algebraic notation, reverse Polish has been found to lead to slower calculations.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

2. Shell Programming and Scripting

How to print different multiple lines after two patterns?

Hello, I need to print some lines as explained below, TXT example 1111 2222 3333 4444 5555 6666 7777 8888 6666 9999 1111 2222 3333 4444 5555 (8 Replies)
Discussion started by: liuzhencc
8 Replies

3. Shell Programming and Scripting

Concatenate select lines from multiple files

I have about 6000 files of the following format (three simplified examples shown; actual files have variable numbers of columns, but the same number of lines). I would like to concatenate the ID (*Loc*) and data lines, but not the others, as shown below. The result would be one large file (or... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. Shell Programming and Scripting

Join common patterns in multiple lines into one line

Hi I have a file like 1 2 1 2 3 1 5 6 11 12 10 2 7 5 17 12 I would like to have an output as 1 2 3 5 6 10 7 11 12 17 any help would be highly appreciated Thanks (4 Replies)
Discussion started by: Harrisham
4 Replies

5. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

6. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

7. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

8. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

9. Shell Programming and Scripting

print lines which match multiple patterns

Hi, I have a text file as follows: 11:38:11.054 run1_rdseq avg_2-5 999988.0000 1024.0000 11:50:52.053 run3_rdrand 999988.0000 1135.0 128.0417 11:53:18.050 run4_wrrand avg_2-5 999988.0000 8180.5833 11:55:42.051 run4_wrrand avg_2-5 999988.0000 213.8333 11:55:06.053... (2 Replies)
Discussion started by: annazpereira
2 Replies

10. Shell Programming and Scripting

Concatenate multiple lines based.

Hello, I have been searching the forum for concatenation based on condition. I have been close enough but not got th exact one. infile: -----DB_Name ABC (X, Y,Z). DB_Name DEF (T). DB_Name GHI (U ,V,W). Desired Output file should be: ---------------------------DB_Name ABC... (8 Replies)
Discussion started by: indrajit_u
8 Replies

Featured Tech Videos