Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have got a problem to solve. I want to search a string in a paragraph, for that i am using following sed command;
sed -e '/./{H;$!d;}' -e 'x;/<search String>/!d;' <input file> > output.txtin the above command Blank space separates the paragraph. The above command will copy the entire paragraph to output.txt if the paragraph contains the search string.
I want to do a conditioning to the above output. i.e. i want to count the number of occurrences of search string and accordingly copy the paragraph to different files.
eg: if <search string> is occurring 2 times in the specified paragraph move to Output2times.txt, and if the string is occurring 3 times then Output3times.txt
Please let me know if anyone can suggest some command, or set of commands to use.
Sample text is given below, and also the search string in the text file.
##############################################
LayerId ; LowOrder ; VC3
ObjectId ; -11757093633
TrailId ; HYDRGRGNDELLL05
CustomerId ; OSP Data
Availability ; High
Activation Policy ; Activate In Network
LinkDistance ; 1
EquipmentCost ; 0
Jitter ; 0
Unavailability ; 0
ErrorRate ; 0
Priority ; 1
State ; NetworkLearned
SubState ; Incomplete - Missing XC
TrailType ; NormalCTP ; ABHYDRXXXXM00116X102 ; #1:0:8:0:1 ; <:9:2> ; Missing XC ;
CTP ; ACHYDRSJGAN00116X101 ; #1:0:6:0:1 ; <:9:2> ; SIMPLE ; CTP ; #1:0:8:0:1 ; <:9:2> ;
CTP ; ABHYDRXXXXM00116X102 ; #1:0:6:0:1 ; <:9:2> ; Missing XC ;
##############################################In the above paragraph, Missing XC is occuring 3 times, and hence need to be copied to Output3times.txt.
Space seperates the paragraph( Also line of '#' is a separator but not usefull)
Thanks in advance..

You write "Missing XC is occuring 3 times, and hence need to be copied to Output3times.txt", but I see Missing XC occuring once in the first paragraph (from LayerId to TrailType) and two times in the second paragraph (where all lines start with CTP). So the first paragraph should be written to Output1times.txt and the second to Output2times.txt, I think.
The fastest way to solve this problem is by using Perl. Set the input-record-separator to the empty string ($/ = "") to achieve reading by paragraph.
This message was produced with 100% recycled electrons

![]() |
need script help
|
How to Read .txt file and...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |