Search for a pattern in a file and split the line into two lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a pattern in a file and split the line into two lines
# 1  
Old 10-12-2015
Search for a pattern in a file and split the line into two lines

Hi All,

Greetings everyone !!!

I have a file which has many lines, out of which one line is as below.

Quote:
declare @newname varchar(30) Select @newname = filename from table_bck
I need to search for pattern "varchar(30) Select" and if exists, then split the line as below.

Quote:
declare @newname varchar(30)
Select @newname = filename from table_bck
I am trying to achieve this in ksh. Can anyone help me on this.

Last edited by Pradhikshan; 10-12-2015 at 01:08 PM..
# 2  
Old 10-12-2015
Hello Pradhikshan,

Could you please try following. Let's say following is Input_file.
Code:
cat Input_file
declare @newname varchar(30) Select @newname = filename from table_bck
declare @newname varchar(30) Select @newname = filename from table_bck
declare @newname varchar(30) Select @newname = filename from table_bck

Then following is the code for same.
Code:
awk '/varchar\(30\) Select/{gsub(/varchar\(30\) /,"&\n");print}'  Input_file

Output will be as follows.
Code:
declare @newname varchar(30)
Select @newname = filename from table_bck
declare @newname varchar(30)
Select @newname = filename from table_bck
declare @newname varchar(30)
Select @newname = filename from table_bck

Thanks,
R. Singh
# 3  
Old 10-12-2015
Using Sing's input file with sed!
Code:
cat split | sed '/\(.*Select \)\(.*$\)/ {s//\1\n\2/}'

declare @newname varchar(30) Select
 @newname = filename from table_bck
declare @newname varchar(30) Select
 @newname = filename from table_bck
declare @newname varchar(30) Select
 @newname = filename from table_bck

I do have a problem including (30), the issue of parens in sed.
If anybody!?
Fas
# 4  
Old 10-12-2015
With a standards conforming version of sed, try:
Code:
sed 's/\(varchar[(]30[)]\) \(Select\)/\1\
\2/' Input_file

producing the output:
Code:
declare @newname varchar(30)
Select @newname = filename from table_bck
declare @newname varchar(30)
Select @newname = filename from table_bck
declare @newname varchar(30)
Select @newname = filename from table_bck

I believe GNU sed would allow \n instead of the \<newline> shown here, but it probably would also have to be told to conform to the standards with something like:
Code:
sed --posix 's/\(varchar[(]30[)]\) \(Select\)/\1\
\2/' Input_file

to get the parenthesized expressions to work correctly. (I don't have a GNU sed available to test at the current time.)
# 5  
Old 10-12-2015
Revised sed statement with proper result.
Code:
cat split | sed '/\(.*varchar.*[)] \)\(.*$\)/ {s//\1\n\2/}' 

declare @newname varchar(30) 
Select @newname = filename from table_bck
declare @newname varchar(30) 
Select @newname = filename from table_bck
declare @newname varchar(30) 
Select @newname = filename from table_bck

Square brackets being the the parens isolator.
# 6  
Old 10-12-2015
Please get rid of the unneeded cat and consider simplifying your sed script as shown in post #4 in this thread.
# 7  
Old 10-12-2015
Thanks for the explanation Don.

But mine is slightly different and still works.
Is my solution improper in any way?
Fas

Saw your post after my answer!

---------- Post updated at 03:04 PM ---------- Previous update was at 03:03 PM ----------

Will do!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

How to split a file based on pattern line number?

Hi i have requirement like below M <form_name> sdasadasdMklkM D ...... D ..... M form_name> sdasadasdMklkM D ...... D ..... D ...... D ..... M form_name> sdasadasdMklkM D ...... M form_name> sdasadasdMklkM i want split file based on line number by finding... (10 Replies)
Discussion started by: bhaskar v
10 Replies

3. Shell Programming and Scripting

Search pattern and write line into another file

Hi, I have a file which contains the below details.. My requirement is to fetch all the lines which are starting with "ABC_XY_" into 1 file and rest of the lines (not starting with "ABC_XY_") into another file. Could you please help with what command needs to be used? file1.txt ----------... (12 Replies)
Discussion started by: satyaatcgi
12 Replies

4. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

7. Shell Programming and Scripting

Search a pattern in a file with contents in a single line

Hi all I am searching for a pattern in a file . The file content is in a single line.If am doing a grep or sed for the a particular pattern am getting whole file. I want the result in different lines. attaching the file for reference search pattern "/xxxxxx/hhhh/tttttttt/sss/" and... (4 Replies)
Discussion started by: sparks
4 Replies

8. Shell Programming and Scripting

Split File Based on Line Number Pattern

Hello all. Sorry, I know this question is similar to many others, but I just can seem to put together exactly what I need. My file is tab delimitted and contains approximately 1 million rows. I would like to send lines 1,4,& 7 to a file. Lines 2, 5, & 8 to a second file. Lines 3, 6, & 9 to... (11 Replies)
Discussion started by: shankster
11 Replies

9. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question