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
# 8  
Old 10-12-2015
Quote:
Originally Posted by Klasform
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!
The differences between:
Code:
cat split | sed '/\(.*varchar.*[)] \)\(.*$\)/ {s//\1\n\2/}'

and:
Code:
sed 's/\(varchar[(]30[)]\) \(Select\)/\1\
\2/' Split

are:
  1. the 1st form reads the data in split twice and writes the data in split twice while the 2nd form reads and writes the data once,
  2. the 1st form uses two processes while the 2nd form uses one process,
  3. the 1st form will transform any line containing the string varchar followed by zero of more arbitrary characters followed by a ) and a <space> while the 2nd form changes the <space> character in the string varchar(30) Select to a <newline> character (as specified in the 1st post in this thread),
  4. the 1st form uses more system resources than the 2nd form,
  5. the 1st form runs slower than the 2nd form, and
  6. the 1st form will work on some systems that change a \n in a sed substitute command replacement string to a <newline> character and on systems with a standards conforming version of sed will print varchar(30)nSelect (note the n instead of the requested <newline>) while the 2nd form should do what you want with any sed.

Also, compare the results you get from the two above scripts with the input:
Code:
declare @newname varchar(30) Select @newname = filename from (table_bc)

I assume that your input file won't have anything that has more than one closing parenthesis on a line containing the string varchar, but the 1st form will give you something you don't want if that should ever occur. The 2nd form only transforms the text you said you wanted to transform.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 10-12-2015
It is hard to get this level of specifics elsewhere, why I appreciate your time on this. I will dissect the whole line by line!

Perfect for learning since I just started to be interested by sed and it's flexibility.

Thanks Don
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