Results 1 to 10 of 10
Hi everyone. I am a noob to linux and I need some help. I am running openSUSE 11.2.
I have a simple text file called "test.txt". Here are the contents.
...
- 08-12-2011 #1Just Joined!
- Join Date
- Apr 2006
- Posts
- 16
grep and sed not working as expected. What's wrong?
Hi everyone. I am a noob to linux and I need some help. I am running openSUSE 11.2.
I have a simple text file called "test.txt". Here are the contents.
I want to find and replace "TEST=" whole word only.Code:asdf TEST= TEST="asdf" TEST= TEST= asdf
When I run the command...
I get as the output...Code:grep -w 'TEST=' test.txt
When I run the command...Code:TEST= TEST="asdf" TEST= TEST=
I get in the file...Code:sed -i -e 's/TEST=/blah=/g' test.txt
When I run the command...Code:asdf blah= blah="asdf" blah= blah= asdf
Nothing is changed in the file.Code:sed -i -e 's/\bTEST=\b/blah=/g' test.txt or sed -i -e 's/\<TEST=\>/blah=/g' test.txt
Why doesn't the word boundaries works and the -w for grep?
thanks
- 08-12-2011 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
1. grep does not do replacement (grep: globally look for regular expression and print),
2. word-constituent characters are letters, digits, and the underscore (excerpt from man grep), which will constrainsomewhat.Code:\< \> \b
Consider:
producing:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate string substitution. # Utility functions: print-as-echo, print-line-with-visual-space, debug. # export PATH="/usr/local/bin:/usr/bin:/bin" pe() { for _i;do printf "%s" "$_i";done; printf "\n"; } pl() { pe;pe "-----" ;pe "$*"; } db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; } db() { : ; } C=$HOME/bin/context && [ -f $C ] && $C sed gres FILE=${1-data1} pl " Input data file $FILE:" cat $FILE pl " Results, sed, non-global:" sed 's/TEST=/blah=/' $FILE pl " Results, sed, global:" sed 's/TEST=/blah=/g' $FILE pl " Results, sed, empty string \< \>:" sed 's/\<TEST\>=/blah=/g' $FILE pl " Results, sed, empty string \b \b:" sed 's/\bTEST\b=/blah=/g' $FILE pl " Results, gres:" gres 'TEST=' 'blah=' $FILE exit 0
The sed command was designed to consolidate a number of functions that had been separate commands. One older command, gres, did search and replace (globally look for a regular expression and substitute). It is occasionally handy to have such a work-alike around.Code:% ./s1 Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0.8 (lenny) GNU bash 3.2.39 GNU sed version 4.1.5 gres (local) 1.17 ----- Input data file data1: asdf TEST= TEST="asdf" TEST= TEST= asdf ----- Results, sed, non-global: asdf blah= blah="asdf" blah= TEST= asdf ----- Results, sed, global: asdf blah= blah="asdf" blah= blah= asdf ----- Results, sed, empty string \< \>: asdf blah= blah="asdf" blah= blah= asdf ----- Results, sed, empty string \b \b: asdf blah= blah="asdf" blah= blah= asdf ----- Results, gres: asdf blah= blah="asdf" blah= blah= asdf
See man pages for details, and experiment, experiment, experiment.
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 08-12-2011 #3Just Joined!
- Join Date
- Apr 2006
- Posts
- 16
Hi drl thanks for the reply.
I know that grep doesn't replace. I was demonstrating how the -w doesn't produce the desired results.
What I want is to create a sed command that replaces the whole word only.
So if this is my input file.
Code:asdf TEST= TEST="asdf" TEST= TEST= asdf
I want to run a sed command that changes the file to look like this.
Notice how the 3rd line is not changed because TEST="asdf" is all a single word?Code:asdf blah= TEST="asdf" blah= blah= asdf
I want to only change whole words only.
It seems like the /b /< /> don't work like I want them to.
- 08-12-2011 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Is the "=" part of the definition of a word?
Look carefully at how your sed calls are different from mine ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 08-12-2011 #5Just Joined!
- Join Date
- Apr 2006
- Posts
- 16
In this case yes the "=" is part of the word.
I would like the use of my script to be able to search a file for ANY string to do a find and replace on the whole word only.
- 08-12-2011 #6Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
Yes, I understand that you would like them to be, but they are not in the definition of a word when dealing with regular expressions: 2. word-constituent characters are letters, digits, and the underscore (excerpt from man grep) ...
They can be part of string, but not of a word.
If you look carefully at how I placed them in the demo script, you will see I did not include anything non-alpha-numeric when using.Code:\< \> \b
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 08-12-2011 #7Just Joined!
- Join Date
- Apr 2006
- Posts
- 16
Okay after doing some digging it seems as though this might be impossible to do with SED and/or gawk. Even open office and Microsoft word cannot accomplish this.
I would like to be able to do a find and replace on ANY ASCII character word that is a WHOLE word.
For instance what if a user wants to search for...
in text that is something like...Code:()(""%#$'*&!@'
and they want this exact whole word.Code:blah blah blah ()(""%#$'*&!@' blah blah blah
Basically to me the term "whole word" means a string of any ASCII character allowed that is delimited by a white space.
It doesn't seem like it should be limited to only letters from A-Z and a-z and numbers 0-9
I guess I will have to write a function in c.
Thanks for your help.
- 08-13-2011 #8Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
I created a perl script to address the issue of substituting general strings without regard to properties of the characters in the context of regular expressions.
This issue not peculiar to SuSE so I posted the solution in thread http://www.linuxforums.org/forum/pro...tml#post856394
The driver script shows the operation of the utility.
Somewhat parenthetically, I should add that this kind of operation could be done with sed and awk, but one would need to escape the characters that have special meaning as regex meta-characters. The perl code seems like an easier solution.
The code itself is longish, although most of it deals with argument processing and modest checks for errors and consistency. The actual heart of the code could be done in a few lines, but I spread things out so that 1) one could see how it operates, and 2) to include a global option like sed has for substitution.
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 08-16-2011 #9Just Joined!
- Join Date
- Apr 2006
- Posts
- 16
Hi drl.
I appreciate your help on this.
After checking on this, I found that my linux distro is running BusyBox, that seems to not have GNU SED.
When I run the -r option, there is a complaint about the regex.
Here is a regular expression that I have devised that seems to work on the GNU SED.
Take a look at this and see what you think.
Consider the input file. Only spaces are used for white spaces.
Now consider the regexCode:test= test="something" test= test= test= test= test=
This seems to work for me. Do you see any problems?Code:test=((?!.)|(?=\s)+)
- 08-16-2011 #10Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
If it works for you on samples of your regular data, then you have a solution -- good work.
Originally Posted by karozans
Best wishes ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote
