The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
string deletion, variable contents, fixed delimiters rebelbuttmunch Shell Programming and Scripting 2 03-24-2009 07:44 AM
script not working...select utility mobydick Shell Programming and Scripting 6 05-08-2008 07:33 AM
Encountered error! puspendu UNIX Benchmarks 0 09-07-2006 05:37 AM
Newline character not working for ksh sanikv Shell Programming and Scripting 11 06-23-2005 02:34 PM
554 Unallowed chars encountered abibbens UNIX for Advanced & Expert Users 1 01-29-2002 01:21 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-02-2009
Balaji PK Balaji PK is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 8
select contents between two delimiters (not working if newline in encountered)

Hi,

I am facing difficulties in selecting the contents between two delimiters when there is a new line occurs..

Eg:

Code:
>more sample.txt
abcd -- this is the first line %
efgh-- this is the 
second line and not 
able to print %
ijkl -- this is the 3rd line %


when i search for abcd and use the below command , i am able to print the desired contents between '--' and '%'.
But the same i am not able to print in the case of 'efgh' . Its only printing the 1st line of that word found.

pls tell me what correction to be included in this code.. or suggest me the code for this .

Code:
grep -w efgh sample.txt | awk -F"-" '{print $2}' | awk -F"||" '{print $1}'



Required output : this is the
second line and not
able to print


thanks in advance.
:-)

Last edited by Balaji PK; 11-02-2009 at 05:46 AM.. Reason: code tags, please...
  #2 (permalink)  
Old 11-02-2009
ahmad.diab's Avatar
ahmad.diab ahmad.diab is offline
Registered User
  
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 250
code


Code:
 nawk  '1' ORS="  "  file.txt | nawk '{print $4}' FS="--|%"

the above code will print :-

"this is the second line and not able to print" in one line.

BR
  #3 (permalink)  
Old 11-02-2009
Balaji PK Balaji PK is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 8
This is fine.. but i want it generalised. I mean i should be able to give input text to it..

---------- Post updated at 05:10 AM ---------- Previous update was at 05:08 AM ----------


Code:
#sed -n '/efgh/,/%/{s/efgh-- //;s/%//;p;}' sample.txt

This gives the desired output for that 'efgh'
But does not give correct output for 'abcd'..
pls suggest


Code:
sed -n '/abcd/,/%/{s/efgh-- //;s/%//;p;}' sample.txt
abcd -- this is the first line
this is the
second line and not
able to print
[KRYPTON]/appltgb/test/repc/balaji >



---------- Post updated at 05:23 AM ---------- Previous update was at 05:10 AM ----------

can u pls suggest a generalised code for the problem stated above.

Last edited by pludi; 11-02-2009 at 06:16 AM.. Reason: code tags, please...
  #4 (permalink)  
Old 11-02-2009
ahmad.diab's Avatar
ahmad.diab ahmad.diab is offline
Registered User
  
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 250
code


Code:
nawk  '1' ORS="  "  input_file | nawk '{print $2,$4,$6}' OFS="\n"  FS="--|%" > output_file


Code:
more input_file:-

abcd -- this is the first line %
efgh-- this is the 
second line and not 
able to print %
ijkl -- this is the 3rd line %

more output_file:-

this is the first line
this is the  second line and not  able to print
this is the 3rd line

  #5 (permalink)  
Old 11-02-2009
Balaji PK Balaji PK is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 8
well this is good. But when i change the input file ,,i mean if i insert two more lines..it will not work..pls suggest a code that will help me even if i change the input file. the objecive is to give a input file , a word to search in it. once the word found then print the contents corresponding to the word from '--' to '%' and stop.
Am i clear . ?
  #6 (permalink)  
Old 11-02-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Why don't you try something for yourself with all the the solutions given to you?

This is not a helpdesk service, we're not here to do your work for you.
  #7 (permalink)  
Old 11-02-2009
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
try:

Code:
#  sed -n '/efgh/,/%/{s/efgh-- //;s/%//;p;}' sample.txt
this is the
second line and not
able to print

HTH

Last edited by Tytalus; 11-02-2009 at 05:58 AM.. Reason: forgot to remove the %
Reply

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 05:56 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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