The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extract a string from main string madhu.it Shell Programming and Scripting 1 10-06-2008 04:05 AM
extract a sub string from a main string madhu.it Shell Programming and Scripting 5 10-04-2008 02:22 AM
Extract content from several txt-files larsu Shell Programming and Scripting 7 06-17-2008 03:52 AM
Content extract of a file using awk nr_shan Shell Programming and Scripting 5 12-19-2007 05:22 AM
How to extract a portion of a string from the whole string ds_sastry UNIX for Dummies Questions & Answers 2 09-29-2001 10:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-25-2008
bala041 bala041 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
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 (permalink)  
Old 11-25-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,257
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 (permalink)  
Old 11-25-2008
bala041 bala041 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
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 (permalink)  
Old 11-25-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,257
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 (permalink)  
Old 11-25-2008
bala041 bala041 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
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 (permalink)  
Old 11-25-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,257
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 (permalink)  
Old 11-25-2008
npatwardhan npatwardhan is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 135
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 01:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0