Help to extract the content in this string


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help to extract the content in this string
# 1  
Old 11-25-2008
Help to extract the content in this string

Hi All,

I have a string in the following format:
"kf skjgsdjgngnsd sdgsd

ksjgbksjgb kjsbgsjdgb
dfhdh

sjgsjg j jsbekgjsbdkgj".

In this format i want to extract the contents which is in between the two empty lines: i.e, i want to extract the following
"ksjgbksjgb kjsbgsjdgb
dfhdh ".

Please help me in this. Thanks in advance.

Regards,
Bala
# 2  
Old 11-25-2008
What did you try so far?

Code:
sed -n '/^$/,/^$/ {/^[^ ]/p}' infile
ksjgbksjgb kjsbgsjdgb
dfhdh


Last edited by zaxxon; 11-25-2008 at 02:01 AM.. Reason: Corrected a little error
# 3  
Old 11-25-2008
Hi,
I'm a beginer in UNIX. I have tried using cut with regular expressions. but that doesnt work.The Problem is i'll not be having the string in a file. It comes out of some other operations and the format of that o\p is as given below in the previous post.
can u please explain me the command u gave, what is the use of that infile. The String will be dynamic. it can be anything but only the pattern will be same. That is i have to extract all the string between two new lines.

Thanks in advance,
Bala
# 4  
Old 11-25-2008
You should be able to pipe your output just into that sed command.
infile is just the filename I saved your example on my harddisk and I feed sed not with a pipe in front of it but just with this infile.

If the output you want to parse varies a lot, you can't go like that. You have to know how your output looks as close as possible.

Code:
sed -n              # Print only the stuff we want to see; else it prints all 
                    # the stuff it is processing additionally
'                   # Here starts the regex and commands
/^$/,/^$/           # The / starts and ends a pattern; ^ is the start of a
                    # line, $ the end of a line and since nothing is specified 
                    # between it, it must be an empty line. So writing two 
                    # patterns separated with a comma is like giving a 
                    # range "from" "to"
{/^[^ ]/p}          # When the range is separated, do the command in curled
                    # braces. The command inside the braces says to get a
                    # pattern, that does not start with a blank. Inside square brackets the ^ means "not". The p simply stands for print.


' infile

# 5  
Old 11-25-2008
Hi,
I tried the following thing.
comment1 = $(echo "$comment1" | sed -n '/^$/,/^$/ {/[^ ]/p}').

where comment1 has the string which shuld be formatted. But it's saying that:
sed: command garbled: /^$/,/^$/ {/[^ ]/p}.

Is there anything wrong with the regex we are using.

Regards,
Bala
# 6  
Old 11-25-2008
I am not sure what you are going to do. If you echo "$comment1", what do you get? Is the output you want to parse in a variable? If it is just the output of a command, try using that command and put a pipe | behind that command followed by the sed command. If it is a file, use it as I did above. If there is no reason to force the text into a variable, don't do it.
# 7  
Old 11-25-2008
try saving that string to a file.its easier that way even though you have to create a file.

you can then use:

string = `more <filename>`

then use the string variable to extract that text with the sed command.

you can always delete the file once you are done with rm.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract content between strings

Hello i am stuck with this. i have input which is as follows /type/work /works/OL10627594W 3 2019-04-24T16:46:21.351549 {"created": {"type": "/type/datetime", "value": "2009-12-11T03:18:17.488715"}, "title": "Tog the dog", "covers": , "last_modified": {"type":... (3 Replies)
Discussion started by: ahfze
3 Replies

2. Solaris

Extract content of .dump file

We have been provided a .dump file.The need is to extract the contents(may includes files and folder). ls -lZ didnt help me as Z is not a valid option. How to extract the file contents ? (7 Replies)
Discussion started by: vinil
7 Replies

3. Shell Programming and Scripting

Extract Content from a file

I have an input file with contents like: ./prbru6/12030613.LOG:24514|APPL|prbru6.8269.RTUDaemon.1|?|13:49:56|12/03/06|GMT+3|?|RTUServer Error:Count of Internal Error Qty (-1) < 0, for Audit group id - 1L5XVJ6DQE36AXL, after record number,1, File: EventAuditor.cc, Line: 394|? ... (5 Replies)
Discussion started by: rkrish
5 Replies

4. Shell Programming and Scripting

perl extract content of file

I'm using Mail::Internet module, which will basically filter through email content and extract the body of the message my perl script to extract the body of the email #!/usr/bin/perl -w use Mail::Internet; @lines = <STDIN>; $mi_obj = new Mail::Internet(); ... (2 Replies)
Discussion started by: amlife
2 Replies

5. Shell Programming and Scripting

Extract XML content from a file

310439 2012-01-11 03:44:42,291 INFO PutServlet:? - Content of the Message is:="1.0" encoding="UTF-8"?><ESP_SSIA_ACC_FEED> 310440 <BATCH_ID>12345678519</BATCH_ID> 310441 <UID>3498748823</UID> 310442 <FEED_TYPE>FULL</FEED_TYPE> 310443 <MART_NAME>SSIA_DM_TRANSACTIONS</MART_NAME> 310444... (11 Replies)
Discussion started by: arukuku
11 Replies

6. Shell Programming and Scripting

Extract all the content after a specific data

My input: >seq_1 DSASSTRRARRRRTPRTPSLRSRRSDVTCS >seq_3 RMRLRRWRKSCSERS*RRSN >seq_8 RTTGLSERPRLPTTASRSISSRWTR >seq_10 NELPLEKGSLDSISIE >seq_9 PNQGDAREPQAHLPRRQGPRDRPLQAYA+ QVQHRRHDHSRTQH*LCRRRQREDCDRLHR >seq_4 DRGKGQAGCRRPQEGEALVRRCS>seq_6 FA*GLAAQDGEA*SGRG My output: Extract all... (22 Replies)
Discussion started by: patrick87
22 Replies

7. Shell Programming and Scripting

Extract specific content from a file

My input file: >sequence_1 ASSSSSSSSSSSDDDDDDDDDDDCCCCCCC ASDSFDFFDFDFFWERERERERFSDFESFSFD >sequence_2 ASDFDFDFFDDFFDFDSFDSFDFSDFSDFDSFASDSADSADASD ASDFFDFDFASFASFASFAFSFFSDASFASFASFAFS >sequence_3 VEDFGSDGSDGSDGSDGSDGSDGSDG dDFSDFSDFSDFSDFSDFSDFSDFSDF SDGFDGSFDGSGSDGSDGSDGSDGSDG My... (22 Replies)
Discussion started by: patrick87
22 Replies

8. UNIX for Advanced & Expert Users

sed to extract HTML content

Hiya, I am trying to extract a news article from a web page. The sed I have written brings back a lot of Javascript code and sometimes advertisments too. Can anyone please help with this one ??? I need to fix this sed so it picks up the article ONLY (don't worry about the title or date .. i got... (2 Replies)
Discussion started by: stargazerr
2 Replies

9. Shell Programming and Scripting

Extract content from several txt-files

Hi! Im trying to write a script in ksh that creates a single txt-file from specific content in several other txt-files. From these files I want to extract all text after 'WORD' and before '=', regardless of number of lines and other content. I have tried cat and guess I need... (7 Replies)
Discussion started by: larsu
7 Replies

10. Shell Programming and Scripting

Content extract of a file using awk

Hi Everyone, I have a file with the below content: File1.txt ====== ### ###==> the below table was created for testing1 purpose; ### create table 123 ( field1 date, field2 char(10) primary key(field1) ); ### ###==> the below table was created... (5 Replies)
Discussion started by: nr_shan
5 Replies
Login or Register to Ask a Question