Extract part of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract part of file
# 8  
Old 08-23-2013
Code:
awk '($1=="Insert") {i=1} /\(/ {b=1;next} /\)/{b=i=0} i&&b {print}' file

# 9  
Old 08-23-2013
Hello krishmaths,

Thanks for your solution, but it does not give any output.

I observed my file and it came to this conclusion

INSERT into table (
x
,x
,x
,x
)VALUES
This is the observed pattern in most of files, Can we just get the lines between INSERT and VALUES keywords.
# 10  
Old 08-23-2013
Remember unix is case sensitive. If your insert commands begin with INSERT then use the below

Code:
awk '($1=="INSERT") {i=1} /\(/ {b=1;next} /\)/{b=i=0} i&&b {print}' file

# 11  
Old 08-23-2013
Code:
awk '/\)/exit} f; /\(/ && /Keyword/ {f=1}'  file

Here you can change Keyword to anything you like, like INSERT

NB If Keyword is on different line than (, you need this
Code:
awk '/Keyword/ {t=1} /\)/{exit} f; /\(/ && t {f=1}'  file


Last edited by Jotne; 08-23-2013 at 07:36 AM..
# 12  
Old 08-23-2013
No Solutions are working

Krishmaths - the solution provided by you is giving no output

Jotne - the solution provided by you is giving error
awk: /\)/exit} f; /\(/ && /INSERT/ {f=1}
awk: ^ syntax error

The original file looks like this

Code:
USING CHARACTER SET UTF8 DEFINE JOB 

(

DEFINE SCHEMA Flat_File_Schema
(
  cntnt_id VARCHAR(10)
);

DEFINE OPERATOR o_mload
 TYPE update
 SCHEMA *
 ATTRIBUTES
  (
    VARCHAR TdpId           = @TdpId
  );
DEFINE OPERATOR o_tpump
 TYPE stream
 SCHEMA *
 ATTRIBUTES
  (
    VARCHAR TdpId           = @TdpId
  );
DEFINE OPERATOR o_bteq
 TYPE inserter
 SCHEMA *
 ATTRIBUTES
  (
    VARCHAR TdpId           = @TdpId
  );
DEFINE OPERATOR o_fastload
 TYPE load
 SCHEMA *
 ATTRIBUTES
  (
    VARCHAR TdpId           = @TdpId
   ,VARCHAR UserName        = @UserName 
  );
DEFINE OPERATOR Read_Operator
 TYPE DATACONNECTOR PRODUCER
 SCHEMA Flat_File_Schema
 ATTRIBUTES
  (
    VARCHAR FileName         = @FileName 
);

APPLY
( 'INSERT INTO cntnt_cat_itm (
  cntnt_id
 ,cat_itm_nm
 ,prnt_cat_itm_nm
 ,updt_dt_tm
 ,load_dt_tm
 ,src_tbl_nm
 ,trans_type_cd
 ,jrnl_seq_nbr
 ,commit_locl_tm_zn
 ,batch_dt
 ,batch_id
 ) VALUES
(
  :cntnt_id
 
 
 );'
)
    IGNORE DUPLICATE ROWS
    SELECT * FROM OPERATOR (Read_Operator[@ReadInst]);
);

I just want the lines highlighted
# 13  
Old 08-23-2013
I guess the solutions work for the requirement posted in post #1.

For the "new" requirement you have now, use the below code.

Code:
awk '/INSERT/ {i=1;next} /\)/ {i=0} i{print}'  file

In your first post, you had Insert and ( in different lines and now you have in the same line.
# 14  
Old 08-23-2013
Having real life data helps allot, why did you not post it before Smilie
Now script only looks for end ) after pattern and ( is found.

Code:
awk '/APPLY/ {t=1} f && /\)/{exit} f; /\(/ && t {f=1}' file
  cntnt_id
 ,cat_itm_nm
 ,prnt_cat_itm_nm
 ,updt_dt_tm
 ,load_dt_tm
 ,src_tbl_nm
 ,trans_type_cd
 ,jrnl_seq_nbr
 ,commit_locl_tm_zn
 ,batch_dt
 ,batch_id

@krishmaths: your solution does not handle if trigger word is on other line than (
This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

2. Programming

Extract part of an archive to a different file

I need to save part of a file to a different one, start and end offset bytes are provided by two counters in long format. If the difference is big, how should I do it to prevent buffer overflow in java? (7 Replies)
Discussion started by: Tribe
7 Replies

3. UNIX for Advanced & Expert Users

Need help to extract part of the string

Hi, I have a string with name as 20140412-s1-Potopolive_promos_20140412. So I want to extract only Potopolive string. Could you please help me the command. O/p : Potopolive Thx in advance (5 Replies)
Discussion started by: lkeswar
5 Replies

4. Shell Programming and Scripting

Extract the part of sequences from a file

I have a text file, input.fasta contains some protein sequences. input.fasta is shown below. >P02649 MKVLWAALLVTFLAGCQAKVEQAVETEPEPELRQQTEWQSGQRWELALGRFWDYLRWVQT LSEQVQEELLSSQVTQELRALMDETMKELKAYKSELEEQLTPVAEETRARLSKELQAAQA RLGADMEDVCGRLVQYRGEVQAMLGQSTEELRVRLASHLRKLRKRLLRDADDLQKRLAVY... (8 Replies)
Discussion started by: rahim42
8 Replies

5. Shell Programming and Scripting

Extract part of a string

I have a file with 100s of lines of text. I want to perform an extraction of this line: Info bpzs(pid=2652) using 1000 bits I have not been able to extract it. Should I try expr match or is there another method? This line has data both in front of and in back of it. I do not have grep -o... (5 Replies)
Discussion started by: newbie2010
5 Replies

6. Shell Programming and Scripting

how to extract a certain part of a line

Hi friends, I want to select and use the certain part of a line. For example I have the following line home/1245/hgdf/acsdf/myhome/afolder/H2O/endfile how can I extract the part " /myhome/afolder/H2O/endfile " thanks (6 Replies)
Discussion started by: rpf
6 Replies

7. Shell Programming and Scripting

extract part of text file

I need to extract the following lines from this text and put it in different files. From xxxx@gmail.com Thu Jun 10 21:15:46 2010 Return-Path: <xxxxx@gmail.com> X-Original-To: xxx@localhost Delivered-To:xxxx@localhost Received: from ubuntu (localhost ) by ubuntu (Postfix) with ESMTP... (11 Replies)
Discussion started by: waxo
11 Replies

8. Shell Programming and Scripting

extract last part of string.

Hi, I have a string like this BUNDLE=/home/bob/flx/user.bun how to extract only the the last part ie, only user.bun (2 Replies)
Discussion started by: vprasads
2 Replies

9. Shell Programming and Scripting

How to extract certain part of log file?

Hi there, I'm having some problem with UNIX scripting (ksh), perhaps somebody can help me out? For example: ------------ Sample content of my log file (text file): -------------------------------------- File1: .... info_1 ... info_2 ... info_3 ... File2: .... info_1 ... info_2 ...... (10 Replies)
Discussion started by: superHonda123
10 Replies

10. UNIX for Dummies Questions & Answers

Extract a part of file name

Hi, I want to extract a part of filename and pass it as a parameter to one of the scripts. Could someone help. File name:- NLL_NAM_XXXXX.XXXXXXX_1_1.txt. Here i have to extract only XXXXX.XXXXXXX and the position will be constant. that means that i have to extract some n characters from... (6 Replies)
Discussion started by: dnat
6 Replies
Login or Register to Ask a Question