Match exactly a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match exactly a string
# 1  
Old 07-03-2017
Match exactly a string

I am formatting my code and for that I am trying to write a script which can quicken some repetitive work.

I need to match "==" exactly in a string and replace it by inserting a (single) blank space before and after it.


Sample Strings:

1.
Code:
this.something    =='something'.that

2.
Code:
this.something    == 'something'.that

3.
Code:
this.something=='something'.that

4.
Code:
this.something    ==    'something'.that

5.
Code:
this.something== 'something'.that

Expected Output:

1.
Code:
this.something == 'something'.that


My expression:
Code:
(\w+)\s*(==)\s*(['"]?\w+["']?)

works fine when there's only "==" between two statements but it fails when when it encounters "===". It matches the "==" in "===" and produces output as:

Code:
this.something == =something.that

something that I don't want.

What's confusing me is that the above code seems to work fine for me in Notepad++ but doesn't work in grep/sed etc. Somehow I just feel that I must be doing something silly.

Please provide your suggestions.

Thanks.

Last edited by prohank; 07-04-2017 at 08:08 AM..
# 2  
Old 07-03-2017
Hello prohank,

Could you please try following and let me know if this goes good.
Code:
awk '{sub(/ +==/," == ");print}'   Input_file

Thanks,
R. Singh
# 3  
Old 07-04-2017
Hi R. Singh,

Thanks for your reply.

Actually there could be more than one type of input statements. The provided one is just one of the type.

Let me add some more. Apologies from my part.
# 4  
Old 07-04-2017
Try
Code:
awk '{gsub (/ *==/, "=="); gsub (/==  */, "=="); gsub (/==+/, " & ")}1' file

# 5  
Old 07-04-2017
It's not nice to change your problem statement after someone has posted code that attempts to solve your original problem. Readers now can only guess at the problem that Ravinder was trying to help you solve.

I also note that your sample input lines contain leading spaces, but your sample output does not. Your problem statement doesn't say anything about removing leading whitespace???

Your problem statement is not clear about what should happen if more than one occurrence of == that is not adjacent to another = is present on a single line nor of what should happen to lines that do not contain any occurrences of == that are not adjacent to another =???

When posting in the UNIX & Linux forums, you might frequently hear that using Notepad++ to edit UNIX format text files is a silly mistake and that you should learn to use vi. But, since you clearly can't use grep to edit files, it isn't at all clear what you are really trying to do.

If one were trying to change each line in a file that contains the first of one or more occurrences of a string starting with a character that is not an equal sign followed by zero or more space and/or tab characters followed by exactly two equal signs followed by zero or more space and/or tab characters followed by another character that is not an equal sign so that the two equal signs and the preceding and following spaces and tabs are change to one space, two equal signs and one more space AND any leading space and/or tab characters at the start of that line are removed, and all other lines in th file are printed unchanged, one could try something like:
Code:
awk '
match($0, /[^=][[:space:]]*==[[:space:]]*[^=]/) {
	$0 = substr($0, 1, RSTART) " == " substr($0, RSTART + RLENGTH - 1)
	sub(/^[[:space:]]*/, "")
}
1' file

Of course, you also didn't bother telling us what operating system and shell you're using (which could be very important for this problem if one were to suggest code using awk or sed. If you want to try the above code on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 07-04-2017
Thanks RudiC.

I tried your solution but its failing for this case:
input:
Code:
this.something    	                     == 'something'.that

returns:
Code:
this.something           == 'something'.that


Hi Don,

Quote:
It's not nice to change your problem statement after someone has posted code that attempts to solve your original problem.
My original problem was still the same, its just that I wrongly expected the reader to identify other scenarios just by one sample input and a statement and I apologized for it too. (I am not a native English speaking person)

Quote:
Your problem statement doesn't say anything about removing leading whitespace???
I don't need to remove the leading blank spaces. I will correct it.


Quote:
Your problem statement is not clear about what should happen if more than one occurrence of == that is not adjacent to another = is present on a single line nor of what should happen to lines that do not contain any occurrences of == that are not adjacent to another =???
Its not in my requirement.


Quote:
When posting in the UNIX & Linux forums, you might frequently hear that using Notepad++ to edit UNIX format text files is a silly mistake and that you should learn to use vi. But, since you clearly can't use grep to edit files, it isn't at all clear what you are really trying to do.
I mentioned "grep" because I was trying to find the pattern initially and if I get the desired result I would run "sed" over it. But "grep" returned lines with "===" also while searching(with my code) for "==", hence the mention.
I am not actually editing unix files. I am just using unix shell commands to format my EJS code.
I am on windows 7 (not my machine) and don't have privileges to install other software so can't use vi. So I'm running script commands through Git Bash. Please don't judge me for it.


Quote:
If one were trying to change each line in a file that contains the first of one or more occurrences of a string starting with a character that is not an equal sign followed by zero or more space and/or tab characters followed by exactly two equal signs followed by zero or more space and/or tab characters followed by another character that is not an equal sign so that the two equal signs and the preceding and following spaces and tabs are change to one space, two equal signs and one more space AND any leading space and/or tab characters at the start of that line are removed, and all other lines in th file are printed unchanged, one could try something like:
Whoa!
Thanks for your time. I thought samples would mean more.

Alas, thanks for your solution Mr Admin.
# 7  
Old 07-04-2017
Quote:
Originally Posted by prohank
Thanks RudiC.

I tried your solution but its failing for this case:
input:
Code:
this.something                             == 'something'.that

returns:
Code:
this.something           == 'something'.that

.
.
.
It doesn't consider <TAB> chars as those were not mentioned in post#1 nor in the examples. Try [[:space:]]* in lieu of the two * in my proposal.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help match the exact string

I just want to match "binutils1_test" only, and print the match line only lyang001@lyang001-OptiPlex-9010:/tmp$ cat file zbinutils1_test bbinutils1_test binutils1_test w-binutils1_test lyang001@lyang001-OptiPlex-9010:/tmp$ cat file |grep -w 'binutils1_test' ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

2. Shell Programming and Scripting

Get the exact match of the string!

Hi All, I am breaking my head in trying to get a command that will exactly match my given string. I have searched net and found few of the options - grep -F $string file grep -x $string file grep "^${string}$" file awk '/"${string}"/ {print $0}' file strangely nothing seems to... (3 Replies)
Discussion started by: dips_ag
3 Replies

3. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

Match a string but with little difference

I want to output the records matched with a string like "abcdefg", but with one letter difference at any position. Can I do this with awk or grep? Thank you! (1 Reply)
Discussion started by: xshang
1 Replies

5. Shell Programming and Scripting

How to print everything after a string match

Hi all, I'm trying to do some work on the authorized_keys file to do a check if there's any information after the hash key.. At the end of the hash key's in the file, there can be an = or == Is there a way to check if anything exists after these equals and if so print it out or else print... (2 Replies)
Discussion started by: Jazmania
2 Replies

6. Shell Programming and Scripting

How can I match the particular character in the string?

Hi, I want to check out a word in the text file and generate a clear report for me to see... The text file content: Content: ............ 20120608: 20120608: ............ 20120608: .......... 2012031201: , hime] End of the file My expected output is: Full TXT: manatsu TXT:... (3 Replies)
Discussion started by: meroko
3 Replies

7. Shell Programming and Scripting

pattern match in a string

Hello, Please see below line code: #!/bin/ksh set -x /usr/bin/cat /home/temp |while read line do if ] then echo "matched" else echo "nope" fi done content of filr temp is as below (4 Replies)
Discussion started by: skhichi
4 Replies

8. UNIX for Dummies Questions & Answers

How do mask off the string that match my value?

Hi, I have a file like following, aaabb aaavv bbdddaaab fgdgjhaa bfd 12352aa dgs1xaf sdgsdyg4 How can i get the output below(mask off all the line that have "a") by using vim #aaabb #aaavv #bbdddaaab #fgdgjhaa bfd #12352aa (4 Replies)
Discussion started by: 793589
4 Replies

9. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

10. Shell Programming and Scripting

Perl: Better way to match string within a string

Hi, I'm trying to get one field out of many as follows: A string of multiple fields separated with "/" characters: "/ab=12/cd=34/12=ab/34=cd/ef=pick-this.one/gh=blah/ij=something/" I want to pick up the field "ef=pick-this.one" which has no regular pattern except it starts with "ef=xxxx"... (3 Replies)
Discussion started by: Juha
3 Replies
Login or Register to Ask a Question