1

As the title says, i'd like to find a string in a text, but the string is special. The text may like this:

{
PREBINDING = NO;
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "22B333C5-6D7A-4A76-81F2-12A045DAE44C";
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = 2;
}

( Additionally, if you are a ios developer, you may know that the text example is from XXX.xcodeproj/project.pbxproj )

I need to find and delete this line of text:

"PROVISIONING_PROFILE[sdk=iphoneos*]" = "22B333C5-6D7A-4A76-81F2-12A045DAE44C";

In this line, it has quotes("), has brackets([ ]), has star(*), also has space( ). When i try to find this line using code below:

provisionStr="22B333C5-6D7A-4A76-81F2-12A045DAE44C"
fileDir=project.pbxproj

totalLineStr=`grep $provisionStr  $fileDir`
sed -i "" "s@$provisionStr@@" $fileDir

This bash code does not work. Please help me, i'm just beginning to learn to use bash.

pinchwang
  • 353
  • 1
  • 2
  • 13
  • 1
    Shouldn't you be using `$totalLineStr` in your sed command. Also your command will replace the full line with an empty line. If you want to completely remove the line, you can simply do: `sed -i '/22B333C5-6D7A-4A76-81F2-12A045DAE44C/d' $fileDir` – Vivek Dec 08 '12 at 07:00
  • 1
    thank you very much, it can successfully run now. – pinchwang Dec 08 '12 at 07:09
  • but i also want to know, that why can't i use $totalLineStr in sed command? Can you tell me? – pinchwang Dec 08 '12 at 07:10
  • Thats because you have got a lot of sed metacharacters in your pattern - `" [ ] *`. You will have to escape all those in the pattern. – Vivek Dec 08 '12 at 07:35
  • 1
    Those "special characters" are RE meta-characters. If you ever have want to search for a string that can contain them but you don't want them treated as RE meta-characters, do NOT try escaping them as that's error-prone and awkward, just use a tool that does string comparison instead of RE comparison such as `awk 'index(in,str)'` since string comparison is what you REALLY want instead of RE comparison. – Ed Morton Dec 08 '12 at 08:20

2 Answers2

1

sed substitute with nothing:

s(for substitute)/string_to_match/new_string_or_empty_to_remove/g(for global/all document)

sed 's/\"PROVISIONING_PROFILE\[sdk=iphoneos\*\]" = \"22B333C5-6D7A-4A76-81F2-12A045DAE44C\";//g'

escape double quotes, asterix and brackets using \

AWE
  • 4,045
  • 9
  • 33
  • 42
  • How if i don't exactly know what's in this line? i just know 22B333C5-6D7A-4A76-81F2-12A045DAE44C, and i want to delete all lines it belongs to. – pinchwang Dec 08 '12 at 07:13
  • `sed '/pattern to match/d' ./infile` like Vivek suggested or http://stackoverflow.com/questions/5410757/sed-delete-a-line-containing-a-specific-string – AWE Dec 08 '12 at 07:55
1

It sounds like all you want to do is:

grep -v '"22B333C5-6D7A-4A76-81F2-12A045DAE44C"' file

If you ever did want to search for a string in a file instead of search for matches to an RE in a file, then you'd just do this, for example:

$ awk -v str='"PROVISIONING_PROFILE[sdk=iphoneos*]"' 'index($0,str)' file
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "22B333C5-6D7A-4A76-81F2-12A045DAE44C";

$ awk -v str='"PROVISIONING_PROFILE[sdk=iphoneos*]"' '!index($0,str)' file
{
PREBINDING = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = 2;
}

Do not try escaping all the RE metacharacters and then do an RE comparison as that makes no sense. If you want to look for an RE then do an RE comparison but if you want to look for a string do a string comparison, it's that simple.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • You're welcome and i just updated my answer to show how to search for a string in a file rather than search for an RE. – Ed Morton Dec 08 '12 at 08:49