Break a file into smaller pieces with same search string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break a file into smaller pieces with same search string
# 1  
Old 09-24-2018
Break a file into smaller pieces with same search string

please help to break a file into smaller pieces with same search string and delete the matched lines.
like i have a below file
Code:
<expression>
some text
........
..........
</expression>
<expression>
some text
........
..........
</expression>
<expression>
some text
........
..........
</expression>
<expression>
some text
........
..........
</expression>
......................
.......................
.......................

Now every expression "/expression" will break in separate files.
# 2  
Old 09-25-2018
What operating system are you using?

What shell are you using?

What have you tried to solve this problem on your own?

What is the name of your input file?

What names should be used for the output files you hope to create?
These 2 Users Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-25-2018
Here is relevant information you asked.

What operating system are you using?
Linux 7

What shell are you using?
Bash

What have you tried to solve this problem on your own?

I tried some command but not getting expected output

What is the name of your input file?
input.xml

What names should be used for the output files you hope to create?
output.ranx.xml
# 4  
Old 09-25-2018
I am extremely disappointed that after observing us helping other users for more than 2.5 years and helping you with two other problems, you are unwilling to clearly show us what output(s) you're trying to produce, to show us what you have tried, and to show us the output(s) that your attempt(s) have produced.

If I am correctly interpreting your request to be to copy lines from a file named input.xml into a file named output.ranx.xml with all sequences of lines in your input file starting with a line containing the string <expression> up to and including a line containing the string </expression> deleted, you could try something like:
Code:
sed '/<expression>/,/<\/expression>/d' input.xml > output.ranx.xml

which with your given sample input would create a file named output.ranx.xml containing the text:
Code:
......................
.......................
.......................

When I first read your post #1, I had guessed that you wanted to create multiple output files with each of those output files containing one matched set of lines starting with a line containing the string <expression> and ending with the string </expression> but since you have specified that there is to only be one output file, that obviously can't be what you're trying to do. And re-reading your first post, I also see that this would not delete the matched lines as requested, so that can't be what you want.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-25-2018
Apologies, I think i you missed or i didnt convey my message correctly .. I have one input file "input.xml" that contains the above text and it search the expression first and import to the 1_input_ranx.xml file and expression second and import to the 2_input_ranx.xml so on and remove the expression receptively. So we need to create multiple output files from one input file with the same expression. Hope i can elaborate it now.Thanks!
# 6  
Old 09-25-2018
We obviously have a little bit of a language barrier here. I have no idea what "remove the expression receptively" means. So, I do not know what output you want to place into any of your output files.

Since you can't do arithmetic in sed, you'll probably want to process your input file directly in the shell or use something like awk or perl.

Can we assume that every input line containing <expression> will have a line following it containing </expression>?

Please try to come up with something that does what you want and show us what you have tried to get there. And, please show us exactly what output you hope to produce in each of the output files you want to produce from your sample input!
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 09-25-2018
below is the Input file

Code:
<BEAM-PATTERN-LIST>
<bean pattern>
<M>0.0,0.00</M>
<M>1.0,0.00</M>
<M>2.0,0.00</M>
<M>3.0,0.00</M>
<M>4.0,0.00</M>
<M>5.0,0.10</M>
<M>6.0,0.10</M>
<M>7.0,0.10</M>
<M>8.0,0.20</M>
<M>9.0,0.20</M>
</bean pattern>
<bean pattern>
<M>170.0,26.50</M>
<M>171.0,26.40</M>
<M>172.0,26.30</M>
<M>173.0,26.30</M>
<M>174.0,26.20</M>
<M>175.0,26.10</M>
<M>176.0,26.10</M>
<M>177.0,26.10</M>
<M>178.0,26.00</M>
<M>179.0,26.00</M>
</bean pattern>
<bean pattern>
<M>190.0,26.00</M>
<M>191.0,26.10</M>
<M>192.0,26.10</M>
<M>193.0,26.10</M>
<M>194.0,26.20</M>
<M>195.0,26.30</M>
<M>196.0,26.30</M>
<M>197.0,26.40</M>
<M>198.0,26.50</M>
<M>199.0,26.50</M>
</bean pattern>
---------------
---------------
------so on-----------
-----------------
</BEAM-PATTERN-LIST>

out put files
first file:
Code:
<M>0.0,0.00</M>
<M>1.0,0.00</M>
<M>2.0,0.00</M>
<M>3.0,0.00</M>
<M>4.0,0.00</M>
<M>5.0,0.10</M>
<M>6.0,0.10</M>
<M>7.0,0.10</M>
<M>8.0,0.20</M>
<M>9.0,0.20</M>

second output file
Code:
<M>170.0,26.50</M>
<M>171.0,26.40</M>
<M>172.0,26.30</M>
<M>173.0,26.30</M>
<M>174.0,26.20</M>
<M>175.0,26.10</M>
<M>176.0,26.10</M>
<M>177.0,26.10</M>
<M>178.0,26.00</M>
<M>179.0,26.00</M>

third out file
Code:
<M>190.0,26.00</M>
<M>191.0,26.10</M>
<M>192.0,26.10</M>
<M>193.0,26.10</M>
<M>194.0,26.20</M>
<M>195.0,26.30</M>
<M>196.0,26.30</M>
<M>197.0,26.40</M>
<M>198.0,26.50</M>
<M>199.0,26.50</M>

---------------
-------nth output file-----------
------------------

hope i make clear this one
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

3. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

7. UNIX for Dummies Questions & Answers

add a string to a file without line break

I searched and found "echo -n" and "printf" are solution for this, but they are not here: $ echo "hello" >> test $ cat test hello $ echo -n "world" >> test $ cat test hello world $ echo -n " seriously?" >> test $ cat test hello world seriously? This is not successful... (15 Replies)
Discussion started by: stunn3r
15 Replies

8. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

9. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies

10. Shell Programming and Scripting

how to insert line break + string in vi (search & replace )

Hello all i have big test file that has allot of structure text something like this : <foo1 *.html> <blah action> somthing 1 somthing 2 </blah> </foo1 > now i will like to insert 2 more lines of text below the <blah action> so it will be like : <foo1... (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question