regex to remove commentaries and blanks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting regex to remove commentaries and blanks
# 1  
Old 06-18-2009
regex to remove commentaries and blanks at the end of the line

Hi all,

I need to prune a var's content as follows:

VAR='blah blah # seew seew'

NEWVAR='blah blah'

(without blanks)

I need also to perform this change by using variable substitution within bash shell. I've tried it with the following subst:

VAR2=${VAR/ \#*/}

but the blanks still remain... Any ideas are welcome.

Thanks in advance

Last edited by yomaya; 06-18-2009 at 07:34 AM..
# 2  
Old 06-18-2009
do you want something like this...
Code:
var2=`echo $var | tr -d ' '`

# 3  
Old 06-18-2009
Thanks, but the text to preserve has also blanks... Maybe I should have been more precise...

Code:
VAR='blah blah blah      # seew seew'
         ^    ^
        
NEWVAR='blah blah blah' 
            ^    ^     (<- should get preserved)


Last edited by yomaya; 06-18-2009 at 07:15 AM..
# 4  
Old 06-18-2009
I dont get you. Give some meaningful example.

Last edited by rakeshawasthi; 06-18-2009 at 07:30 AM..
# 5  
Old 06-18-2009
Quote:
Originally Posted by rakeshawasthi
I dont get you. Give some meanigful example.
Code:
VAR='blah blah blah      # seew seew'
         ^    ^
        
NEWVAR='blah blah blah' 
            ^    ^     (<- should get preserved)

The subchain that should be removed is:
Code:
'      # seew seew'

# 6  
Old 06-18-2009
something like this ??

Code:
echo "'blah blah blah      # seew seew'" | sed 's/\(.*\)#.*/\1/g' | sed 's/.[ ]*$//g'

# 7  
Old 06-18-2009
Quote:
Originally Posted by panyam
something like this ??

Code:
echo "'blah blah blah      # seew seew'" | sed 's/\(.*\)#.*/\1/g' | sed 's/.[ ]*$//g'

Thank you very much. It does the job properly!

Do you know if it's possible to do this without invoking 'sed' (for instance by parameter substitution)? The reason why it would be nicer doing it this way is that VAR is in fact an array (which I didn't mention for clarity) with a few hundred entries.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

awk to remove row 1 and blanks

I am trying to remove $1 along with the blank values from the file. Thank you :). file R_Index Chr Start End Ref Alt Func.IDP.refGene Gene.IDP.refGene GeneDetail.IDP.refGene Inheritence ExonicFunc.IDP.refGene AAChange.IDP.refGene avsnp147 PopFreqMax ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Need regex shell script to remove text from file

Hello I am trying to remove a line like <?php /*versio:2.05*/if (!defined('determinator')){ content goes here}?> Now i want to scan all... (6 Replies)
Discussion started by: devp
6 Replies

4. Shell Programming and Scripting

Perl regex to remove a segment in a line

Hello, ksh on Sun5.8 here. I have a pipe-delimited, variable length record file with sub-segments identified with a tilda that we receive from a source outside of our control. The records are huge, and Perl seems to be the only shell that can handle the huge lines. I am new to Perl, and am... (8 Replies)
Discussion started by: gary_w
8 Replies

5. Shell Programming and Scripting

Remove multiple blanks

Hi, I have data as below And I want output as Thanks (5 Replies)
Discussion started by: Anjan1
5 Replies

6. Shell Programming and Scripting

find regex and remove #

hi , how do i remove # from a line where i found regex.. don't need to remove all the line.. only remove comment.. (3 Replies)
Discussion started by: Poki
3 Replies

7. Shell Programming and Scripting

Using Sed to remove part of line with regex

Greetings everyone. Right now I am working on a script to be used during automated deployment of servers. What I have to do is remove localhost.localdomain and localhost6.localdomain6 from the /etc/hosts file. Simple, right? Except most of the examples I've found using sed want to delete the entire... (4 Replies)
Discussion started by: msarro
4 Replies

8. Shell Programming and Scripting

Regex to remove subdirectory

I need to remove subdirectories that are empty and I've not done this before. First I am going through the files to remove old records. Then if the directory is empty I want to delete it. There are files in /direcotry/images/fs* - 0-9 and a-z The fs* directories need to stay, but any directories... (4 Replies)
Discussion started by: janel10
4 Replies

9. Shell Programming and Scripting

regex to remove text before&&after comma chars

Hi, all: I have a question about "cleaning up" a huge file with regular expression(s) and sed: The init file goes like this: block1,blah-blah-blah-blah,numseries1,numseries2,numseries3,numseries4 block2,blah-blah-blah-blah-blah,numseries,numseries2,numseries3,numseries4 ...... (3 Replies)
Discussion started by: yomaya
3 Replies

10. Shell Programming and Scripting

how to remove trailing blanks, tabs

Hi I need to delete trailing spaces, tabs and unprintable charactes from the file. This file has a number of blank lines which should be left intact. Another words I am trying to remove the junk at the end of each line. Does anyone come across the similar problem? Thanks a lot for any help -A (3 Replies)
Discussion started by: aoussenko
3 Replies
Login or Register to Ask a Question