0

I need help in "grepping" out lines beginning from "TEST FILE" to ";". I'm open to any other approach such as awk

FILE A contains the following:

TEST FILE ABC
this file contains ABC;

TEST FILE DEF
this file contains DEF
;

TEST FILE (DEF) this file contains (DEF);
TEST FILE DGHT this file contains DGHT;

TEST FILE 123
this file contains ABC,
this file contains DEF,
this file contains XYZ,
this file contains KLM
;

I want the following 5 files

File1

TEST FILE ABC
this file contains ABC;

File2

TEST FILE DEF
this file contains DEF
;

File3

TEST FILE (DEF) this file contains (DEF);

File4

TEST FILE DGHT this file contains DGHT;

File5

TEST FILE 123
this file contains ABC,
this file contains DEF,
this file contains XYZ,
this file contains KLM
;
user2008558
  • 341
  • 5
  • 16
  • 1
    Stack Overflow is not a code writing service. Please show your code. Since Stack Overflow hides the Close reason from you: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/).* – jww May 09 '18 at 23:10
  • Possible duplicate of [Split one file into multiple files based on delimiter](https://stackoverflow.com/questions/11313852/split-one-file-into-multiple-files-based-on-delimiter) – Ken Y-N May 09 '18 at 23:46

1 Answers1

0

Following awk may help you on same.

awk '/TEST/{flag=1;++i} flag{print > "file"i} /;$/{flag=""}' Input_file

Adding a non-one liner form of solution too now.

awk '
/TEST/{
       flag=1;
       ++i}
flag{
       print > "file"i}
/;$/{
       flag=""}
'   Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Your solution works.How can I modify your solution to only give me the content of "file5" – user2008558 May 09 '18 at 22:29
  • DOWN VOTER, without giving reason for downvote will not help anyone to improve, so please mention the reason for it. – RavinderSingh13 May 10 '18 at 03:43
  • @user2008558, so if you want to print the contents of `file5` at the last of the code then use `END{system("if [[ -f file5 ]]" ORS "then" ORS "cat file5" ORS "else" ORS "file5 is not present in current directory." RS "fi")}'` before `Input_file` in above code and let me know in case of any queries on same. – RavinderSingh13 May 10 '18 at 03:53
  • Instead of the accepted solution, I'll be grateful to have a solution that will have something like -- awk ' /TEST FILE 123/ where as "TEST FILE 123" (as supposed to TEST) will be the begin search pattern and ";" will be the end search pattern and then output the result to a file (File5) – user2008558 May 10 '18 at 04:05
  • 1
    @user2008558, for simple data from specific place to another specific place following may help `sed -n '/TEST FILE 123/,/;/p' Input_file` but it really depends on your Input_file and actual data. Feel free to open a new thread as it looks like a new question, cheers. – RavinderSingh13 May 10 '18 at 09:06
  • sed -n '/TEST FILE 123/,/;/p' Input_file did get the result I was looking for – user2008558 May 10 '18 at 14:31