Computing.Net > Forums > Unix > using sed

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

using sed

Reply to Message Icon

Name: sanju_1942
Date: October 22, 2007 at 05:25:53 Pacific
OS: WinXP
CPU/Ram: Intel 2GHz/512
Product: Dell
Comment:

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.txt

in 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 ; Normal

CTP ; 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..




Sponsored Link
Ads by Google

Response Number 1
Name: japh
Date: October 24, 2007 at 01:08:12 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More


need script help How to Read .txt file and...



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: using sed

using sed www.computing.net/answers/unix/using-sed/4164.html

Extract text using sed www.computing.net/answers/unix/extract-text-using-sed/5169.html

Loop using SED www.computing.net/answers/unix/loop-using-sed/7471.html