我使用Xcode。我想使用bash批量修改UI字体。
例如更改:
<fontDescription key="fontDescription" type="system" pointSize="17"/>
收件人:
<fontDescription key="fontDescription" name="PingFangSC-Regular" family="PingFang SC" pointSize="12"/>`
要启动的代码:
find . -type f '(' -name '*.storyboard' -o -name '*.xib' ')' . \
-exec sed -i '' s/'\<fontDescription key=\"fontDescription\" type=\"system\" pointSize=\"17\"\/\>'/'\<fontDescription key=\"fontDescription\" name=\"PingFangSC-Regular\" family=\"PingFang SC\" pointSize=\"12\"\/\>'/ {} +
# placeholder, to look code better
这是简单的版本。我想找到数字(字体大小)并在下一个字符串中替换。
我已阅读Using sed to find and replace complex string (preferrably with regex)。
由于@battlmonstr,我尝试了
find . -type f '(' -name '*.storyboard' -o -name '*.xib' ')' \
-exec sed -E -i '' 's@\<fontDescription key=\"fontDescription\" type=\"system\" pointSize=\"17\"/>@<fontDescription key=\"fontDescription\" name=\"PingFangSC-Regular\" family=\"PingFang SC\" pointSize=\"12\"\/>@' {} +
# placeholder
可以。
为了进行更多测试,例如
$ echo abcd123 | sed 's/\([a-z]*\).*/\1/'
abcd
我要保留电话号码。
为了得到号码,然后安排要替换的内容,我尝试了
$ echo "<fontDescription key="fontDescription" type="system" pointSize="17"/>" | \
sed -E 's@\<fontDescription key=\"fontDescription\" type=\"system\" pointSize=\"(\d+)\"\/>@\1@'
<fontDescription key=fontDescription type=system pointSize=17/>
结果不正确,与上面的结果不同。没想到。
我在regex101中进行了测试,没问题。我还在想为什么。
示例内容:
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7yI-IF-sfh">
<rect key="frame" x="126" y="266" width="42" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="PingFangSC-Regular" family="PingFang SC" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vTz-nw-d8Y">
<rect key="frame" x="126" y="416" width="42" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="PingFangSC-Regular" family="PingFang SC" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VCU-bo-Iyu">
<rect key="frame" x="175" y="218" width="42" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="PingFangSC-Regular" family="PingFang SC" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="O3R-uM-fzi">
<rect key="frame" x="91" y="217" width="42" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="PingFangSC-Regular" family="PingFang SC" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="93Z-nX-mmW">
<rect key="frame" x="188" y="64" width="42" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="PingFangSC-Regular" family="PingFang SC" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="9O2-dY-2nt">
<rect key="frame" x="277" y="197" width="42" height="13"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="PingFangSC-Regular" family="PingFang SC" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="7Mp-RI-aG9">
<rect key="frame" x="69" y="87" width="42" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="PingFangSC-Regular" family="PingFang SC" pointSize="12"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<gestureRecognizers/>
<connections>
<outletCollection property="gestureRecognizers" destination="QsQ-yU-xmv" appends="YES" id="4co-bU-mXm"/>
</connections>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
<pongPressGestureRecognizer allowableMovement="10" minimumPressDuration="0.5" id="QsQ-yU-xmv"/>
</objects>
<point key="canvasLocation" x="53.600000000000001" y="26.53673163418291"/>
</scene>
</scenes>
</document>
答案 0 :(得分:4)
在第一版和第二版之间,您已将-i ''
替换为-E ''
,这很可能导致此问题。您可能想保留-i
,并在此之前添加-E
:
sed -E -i '' ...
''
是-i
的一个参数,基本上告诉“不要做备份”。
通常有助于发现问题的方法是将“ sed”与“ find”分开调试,例如单独运行该sed命令,不进行find
且不进行就地替换,然后破解直到工作。然后在顶部添加-i
和find
。
我建议从简单开始,逐步前进。 简化为:
sed -E 's/pointSize="12"/pointSize="15"/'
测试它是否有效。然后改善一下:
sed -E 's/pointSize="[0-9]+"/pointSize="15"/'
测试它是否有效,等等。