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
# 8  
Old 09-25-2018
The following seems to do what you want:
Code:
awk '
$0 == "<bean pattern>" {
	copy = 1
	outFN = ++count "_input_ranx.xml"
	next
}
$0 == "</bean pattern>" {
	copy = 0
	close(outFN)
	next
}
copy {	print > outFN
}' input.xml

This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 09-25-2018
The same with bash builtins:
Code:
#!/bin/bash
x=0 copy=0
while IFS= read line
do
  case $line in
  "<bean pattern>")
    filename=$((++x))_output_ranx.xml
    copy=1
    echo "writing $filename ..."
    exec 3> $filename
    continue
  ;;
  "</bean pattern>")
    copy=0
  esac
  if [ $copy -eq 1 ]
  then
    echo "$line" >&3
  fi
done < input.xml


Last edited by MadeInGermany; 09-25-2018 at 04:50 PM.. Reason: missing continue
This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 09-26-2018
Thanks everyone .. appreciate the help specially Don.. finally i made it but this way..
Code:
dlt=$(awk '/PATTERN-LIST/,/\/PATTERN-LIST/' xmll | grep -v "PATTERN-LIST"|wc -l)
awk '/PATTERN-LIST/,/\/PATTERN-LIST/' xmll | grep -v "PATTERN-LIST" >tmp_file.xml
#echo $dlt
vdlt=$(expr $dlt/736)
vdlt="$(( (dlt) / 736))"
for ((i=0;i<$vdlt;i++));
do
echo $i"_tmp_file.xml";
sed -n '/PATTERN/{p; :loop n; p; /\/PATTERN/q; b loop}' tmp_file.xml >$i"_tmp_file.xml";
vrm=$(sed -n '/PATTERN/{p; :loop n; p; /\/PATTERN/q; b loop}' tmp_file.xml| wc -l);
sed -i -e "1,${vrm}d" tmp_file.xml;
done

# 11  
Old 09-26-2018
If above does what it is supposed to do, fine. If you are happy with it, OK.
But, be aware that it is very inefficient compared to the solutions proposed before in this thread. It creates (6 + 4 * n) processes to run this number of commands (n being the number of lines in the file divided by 736) as opposed to one in Don Cragun's proposal and zero in MadeInGermany's as it's only using bash builtins.
# 12  
Old 09-26-2018
Yes Don proposal also works !!
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