Extract part of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract part of file
# 1  
Old 08-23-2013
Extract part of file

Hello All,


I need to extract part of a file into a new file

My file is
Code:
Define schema xxxxxx

Insert into table
(
a
,b
,c
,d
)
values
(
1,
2,
3,
4,
)

I want to extract only part between first '(' and consecutive ')'
The first '(' will be after the insert statement in the file.
the output should be
Code:
a
,b
,c
,d

Please suggest a solution to do this.

Last edited by Franklin52; 08-23-2013 at 06:23 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 08-23-2013
Code:
awk '($0=="(") {b=1;next} ($0==")") {e=1} b&&!e {print}' filename

or

Code:
awk '/\(/ {b=1;next} /\)/ {exit} b' filename


Last edited by krishmaths; 08-23-2013 at 06:32 AM..
# 3  
Old 08-23-2013
Code:
awk '/\)/{exit} f; /\(/{f=1}'  file
a
,b
,c
,d


Last edited by Jotne; 08-23-2013 at 07:28 AM.. Reason: Removed f=0, not needed
# 4  
Old 08-23-2013
Good one Jotne.

f=0 is not needed as it would exit on finding )
# 5  
Old 08-23-2013
hello,

I might have bracket '(' before the one intended to use.
I want to find insert keyword and then use the bracket '(' after this keyword to extract what ever is in brackets.
# 6  
Old 08-23-2013
@krishmaths`
Good catch
I just modified what I have used before, and added exit to prevent multiple output.
# 7  
Old 08-23-2013
somewhat close to what I need

Code:
awk '/INSERT/{getline;print}' filename

this gives

a

only the first line after the regexp satisfied
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