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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regular Expressions ramky79 UNIX for Advanced & Expert Users 3 05-21-2008 02:13 PM
Help with regular expressions arushunter Shell Programming and Scripting 13 12-23-2006 09:31 PM
regular expressions jack1981 Shell Programming and Scripting 4 07-12-2006 12:10 PM
regular expressions in c++ szzz High Level Programming 2 10-06-2003 07:33 AM
Regular Expressions AresMedia Shell Programming and Scripting 1 08-22-2002 12:55 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-09-2006
Registered User
 

Join Date: Jun 2006
Posts: 29
Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length?

Eg:
Input File san.txt

hello
hi
*User
Good
*Morning
Good
Bye


Applying sed command

sed 's/Reg.Expr/g' san.txt
--------------------------------------->

Desired output

hello
hiUser
GoodMorning
Good
Bye

What should be the value of Reg.Expr i.e. Regular Expression to get the desired output??


Please help
Reply With Quote
Forum Sponsor
  #2  
Old 06-09-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,297
Code:
sed 's/^\*//s' filename
Reply With Quote
  #3  
Old 06-09-2006
Registered User
 

Join Date: Jun 2006
Posts: 29
I tried that but it is not giving the desired output

$ sed 's/^\*//g' san.txt
hello
hi
User
Good
Morning
Good
Bye


Desired Output is
hello
hiUser
GoodMorning
Good
Bye
Reply With Quote
  #4  
Old 06-09-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,297
I don't know how to do that with sed, I guess. awk works:
Code:
awk 'BEGIN{ getline; printf "%s", $0}
    { if(substr($0,1,1)=="*"){
         printf "%s", substr($0,2)
      }
      else {
         printf "\n%s", $0
      }
    }    
     END{printf "\n"} 
     ' filename
Reply With Quote
  #5  
Old 06-09-2006
Registered User
 

Join Date: Jun 2006
Posts: 29
Thank you so much...it worked for me.
I am a newbie to shell scripts and slowly working out my way. I have been trying this since two days but couldnt make out.



Thanks again

Last edited by sandeep_hi; 06-09-2006 at 05:44 AM.
Reply With Quote
  #6  
Old 06-10-2006
tayyabq8's Avatar
Moderator
 

Join Date: Nov 2004
Location: Bahrain
Posts: 555
What about this:
Code:
sed 'N;s/\n\*//;P;D;' san.txt
Regards,
Tayyab
Reply With Quote
  #7  
Old 06-11-2006
Registered User
 

Join Date: Jun 2006
Location: Bangalore, India
Posts: 8
Code:
BEGIN {lastString="";}
{	curStr=$0;
	if (substr(curStr,1,1)=="*") {
		len = length(lastString);
		print substr(lastString,1,len) substr(curStr,2);
	}else{
		print substr(curStr,1)
	}
	lastString = curStr;
}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0