Derived both lines base on pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Derived both lines base on pattern
# 1  
Old 01-05-2016
Derived both lines base on pattern

Dear All,
I have a requests to retrieve all lines if second line last columns meets certain criteria.

Here is the output of the result
Code:
Table: DSSBDW.DA_MASK_CLK_AR
120908 0 86 86 - 1934528 0 70 100 -*-
Table: DSSBDW.DA_MASK_CLK_IP
310657 22030 143 185 - 5281169 7 88 77 *-*
Table: DSSBDW.DA_MASKING
687265 0 574 582 - 18556156 0 97 98 ---
Table: DSSBDW.DA_MASKING_AR
621320 0 287 1281 - 9319800 0 22 22 -**
Table: DSSBDW.DA_MASKING_IP
- - - - - - - - - ---
Table: DSSBDW.DBT_IMT
190872 0 412 412 - 13170168 0 98 100 ---

::For information Table name and next line is first and second line and not continous line.
What I want is to retrieve whenever the second line last column has any asterisk in and position, I will want to retrieve the table name before the line as well as the current line and put it into a single line.

Expected output
Code:
Table: DSSBDW.DA_MASK_CLK_AR 120908 0 86 86 - 1934528 0 70 100 -*-
Table: DSSBDW.DA_MASKING_AR 621320 0 287 1281 - 9319800 0 22 22 -**
Table: DSSBDW.DA_MASK_CLK_IP 310657 22030 143 185 - 5281169 7 88 77 *-*


Thanks.

Last edited by Don Cragun; 01-05-2016 at 04:17 AM.. Reason: Add CODE tags.
# 2  
Old 01-05-2016
Code:
bash-3.2$ awk '/Table/{a=$0}/\*$/{print a,$0}' a.txt
Table: DSSBDW.DA_MASK_CLK_IP 310657 22030 143 185 - 5281169 7 88 77 *-*
Table: DSSBDW.DA_MASKING_AR 621320 0 287 1281 - 9319800 0 22 22 -**

# 3  
Old 01-05-2016
This seems to more accurately produce the requested output:
Code:
awk '/^Table/{a=$0;next}index($NF,"*"){print a,$0}' file

which, with the sample input provided, produces the output:
Code:
Table: DSSBDW.DA_MASK_CLK_AR 120908 0 86 86 - 1934528 0 70 100 -*-
Table: DSSBDW.DA_MASK_CLK_IP 310657 22030 143 185 - 5281169 7 88 77 *-*
Table: DSSBDW.DA_MASKING_AR 621320 0 287 1281 - 9319800 0 22 22 -**

Note, however, that the output here is the order in which the lines appeared in the input file; not in the order you requested in your sample output.
# 4  
Old 01-05-2016
if you want to print the last column has asterisk symbol, then use this..

Code:
bash-3.2$ awk '/Table/{a=$0}$NF~/*/{print a,$0}' a.txt
Table: DSSBDW.DA_MASK_CLK_AR 120908 0 86 86 - 1934528 0 70 100 -*-
Table: DSSBDW.DA_MASK_CLK_IP 310657 22030 143 185 - 5281169 7 88 77 *-*
Table: DSSBDW.DA_MASKING_AR 621320 0 287 1281 - 9319800 0 22 22 -**

# 5  
Old 01-05-2016
Quote:
Originally Posted by itkamaraj
if you want to print the last column has asterisk symbol, then use this..

Code:
bash-3.2$ awk '/Table/{a=$0}$NF~/*/{print a,$0}' a.txt
Table: DSSBDW.DA_MASK_CLK_AR 120908 0 86 86 - 1934528 0 70 100 -*-
Table: DSSBDW.DA_MASK_CLK_IP 310657 22030 143 185 - 5281169 7 88 77 *-*
Table: DSSBDW.DA_MASKING_AR 621320 0 287 1281 - 9319800 0 22 22 -**

That will work on some systems, but it isn't portable. The asterisk is a meta-character in an ERE. On some systems (such as BSD and OS X), you will get an error like:
Code:
$ awk '/Table/{a=$0}$NF~/*/{print a,$0}' file
awk: illegal primary in regular expression * at 
 source line number 1
 context is
	 >>> /Table/{a=$0(NF)~/*/ <<<

instead of the output you got with your version of awk. But, either of the following should work reliably with any standards-conforming awk implementation:
Code:
awk '/Table/{a=$0}$NF~/\*/{print a,$0}' file

escaping the asterisk as you did in post #2 in this thread, or using a matching list bracket expression:
Code:
awk '/Table/{a=$0}$NF~/[*]/{print a,$0}' file

# 6  
Old 01-05-2016
Another approach:
Code:
perl -nle '/\*(-+)?$/ and print "$p $_"; $p=$_' ckwan123.file

Code:
Table: DSSBDW.DA_MASK_CLK_AR 120908 0 86 86 - 1934528 0 70 100 -*-
Table: DSSBDW.DA_MASK_CLK_IP 310657 22030 143 185 - 5281169 7 88 77 *-*
Table: DSSBDW.DA_MASKING_AR 621320 0 287 1281 - 9319800 0 22 22 -**
Table: DSSBDW.DBT_IMT 190872 0 412 412 - 13170168 0 98 100 *--

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

4. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

5. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

6. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. UNIX for Dummies Questions & Answers

sort group of n lines base on pattern in first line

I have record having 10 fields and each field being printed on a new line, first line cotains name of exchange, 2nd line stock name, third line stock price, etc etc... now i want to retrieve data only for a particular exchanged and that too only 2nd and 3rd row info... NSE RNRL 70 12 1... (1 Reply)
Discussion started by: manishma71
1 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. Programming

How to make a function friend to both base and derived class

Hi, I have a base class and derived a class from the base class, i want to print & read the data for the object created for the derived class,so i have overloaded both the << and >> operators and also have done the foward declaration. Below is the code snippet, #include <iostream> class... (3 Replies)
Discussion started by: ennstate
3 Replies
Login or Register to Ask a Question