如何使用sed从文件中的字符串中删除字符串

时间:2014-08-13 19:20:47

标签: java bash sed

我有一个很大的config.ini文件,我希望从该文件中的以下字符串中删除:\start我不想删除:\从所有字符串开始只有下面提到的字符串和文件

reference\:file\:org.eclipse.equinox.http.servlet_1.1.400.v20130418-1354.jar@3\:start

reference\:file\:org.eclipse.equinox.http.registry_1.1.300.v20130402-1529.jar@4\:start

reference\:file\:org.eclipse.equinox.http.jetty_3.0.100.v20130327-1442.jar@3\:start

我对sed很新。 ini文件的内容就像。

org.eclipse.equinox.internal.frameworkadmin.equinox.EquinoxFwConfigFileParser
#Sat Jul 19 04:54:24 GMT 2014
eclipse.p2.profile=DefaultProfile
osgi.framework=file\:plugins/org.eclipse.osgi_3.9.1.v20140110-1610.jar
equinox.use.ds=true
osgi.bundles=reference\:file\:com.jcraft.jsch_0.1.50.v201310081430.jar@4,reference\:file\:com.sun.el_2.2.0.v201303151357.jar@4,reference\:file\:com.tgcs.scrt.p2.touchpoint_1.0.0.201408060721.jar@4,reference\:file\:javax.annotation_1.1.0.v201209060031.jar@4,reference\:file\:javax.el_2.2.0.v201303151357.jar@4,reference\:file\:javax.inject_1.0.0.v20091030.jar@4,reference\:file\:javax.servlet_3.0.0.v201112011016.jar@4,reference\:file\:javax.servlet.jsp_2.2.0.v201112011158.jar@4,reference\:file\:javax.xml_1.3.4.v201005080400.jar@4,reference\:file\:org.apache.ant_1.8.4.v201303080030/@4,reference\:file\:org.apache.batik.css_1.6.0.v201011041432.jar@4,reference\:file\:org.apache.batik.util_1.6.0.v201011041432.jar@4,reference\:file\:org.apache.batik.util.gui_1.6.0.v201011041432.jar@4,reference\:file\:org.apache.commons.codec_1.4.0.v201209201156.jar@4,reference\:file\:org.apache.commons.logging_1.1.1.v201101211721.jar@4,reference\:file\:org.apache.felix.gogo.command_0.10.0.v201209301215.jar@4,reference\:file\:org.apache.felix.gogo.runtime_0.10.0.v201209301036.jar@4,reference\:file\:org.apache.felix.gogo.shell_0.10.0.v201212101605.jar@4,reference\:file\:org.apache.httpcomponents.httpclient_4.1.3.v201209201135.jar@4,reference\:file\:org.apache.httpcomponents.httpcore_4.1.4.v201203221030.jar@4,reference\:file\:org.apache.jasper.glassfish_2.2.2.v201205150955.jar@4,reference\:file\:org.apache.lucene.analysis_3.5.0.v20120725-1805.jar@4,reference\:file\:org.apache.lucene.core_3.5.0.v20120725-1805.jar@4,reference\:file\:org.eclipse.ant.core_3.2.500.v20140203-1328.jar@4,reference\:file\:org.eclipse.compare_3.5.401.v20130709-1308.jar@4,reference\:file\:org.eclipse.compare.core_3.5.300.v20130514-1224.jar@4,reference\:file\:org.eclipse.core.commands_3.6.100.v20130515-1857.jar@4,reference\:file\:org.eclipse.core.contenttype_3.4.200.v20130326-1255.jar@4,reference\:file\:org.eclipse.core.databinding_1.4.1.v20130515-1857.jar@4,reference\:file\:org.eclipse.core.databinding.beans_1.2.200.v20130515-1857.jar@4,reference\:file\:org.eclipse.core.databinding.observable_1.4.1.v20130515-1857.jar@4,reference\:file\:org.eclipse.core.databinding.property_1.4.200.v20130515-1857.jar@4,reference\:file\:org.eclipse.core.expressions_3.4.501.v20131118-1915.jar@4,reference\:file\:org.eclipse.core.externaltools_1.0.200.v20130402-1741.jar@4,reference\:file\:org.eclipse.core.filebuffers_3.5.300.v20130225-1821.jar@4,reference\:file\:org.eclipse.core.filesystem_1.4.0.v20130514-1240.jar@4,reference\:file\:org.eclipse.core.filesystem.linux.x86_1.4.100.v20130430-1334.jar@4,reference\:file\:org.eclipse.core.jobs_3.5.300.v20130429-1813.jar@4,reference\:file\:org.eclipse.core.net_1.2.200.v20130430-1352.jar@4,reference\:file\:org.eclipse.core.net.linux.x86_1.1.300.v20130430-1352.jar@4,reference\:file\:org.eclipse.core.resources_3.8.101.v20130717-0806.jar@4,reference\:file\:org.eclipse.core.runtime_3.9.100.v20131218-1515.jar@2\:start,reference\:file\:org.eclipse.core.runtime.compatibility_3.2.200.v20130326-1255.jar@4,reference\:file\:org.eclipse.core.runtime.compatibility.registry_3.5.200.v20130514-1256/@4,reference\:file\:org.eclipse.core.variables_3.2.700.v20130402-1741.jar@4,reference\:file\:org.eclipse.debug.core_3.8.0.v20130514-0954.jar@4,reference\:file\:org.eclipse.debug.ui_3.9.0.v20130516-1713.jar@4,reference\:file\:org.eclipse.e4.core.commands_0.10.2.v20130515-1857.jar@4,reference\:file\:org.eclipse.e4.core.contexts_1.3.1.v20130905-0905.jar@4,reference\:file\:org.eclipse.e4.core.di_1.3.0.v20130514-1256.jar@4,reference\:file\:org.eclipse.e4.core.di.extensions_0.11.100.v20130514-1256.jar@4,reference\:file\:org.eclipse.e4.core.services_1.1.0.v20130515-1343.jar@4,reference\:file\:org.eclipse.e4.ui.bindings_0.10.102.v20140117-1939.jar@4,reference\:file\:org.eclipse.e4.ui.css.core_0.10.100.v20130515-1857.jar@4,reference\:file\:org.eclipse.e4.ui.css.swt_0.11.0.v20130515-1857.jar@4,reference\:file\:org.eclipse.e4.ui.css.swt.theme_0.9.100.v20130515-1857.jar@4,reference\:file\:org.eclipse.e4.ui.di_1.0.0.v20130515-1857.jar@4,reference\:file\:org.eclipse.e4.ui.model.workbench_1.0.1.v20131118-1956.jar@4,reference\:file\:org.eclipse.e4.ui.services_1.0.1.v20131118-1940.jar@4,reference\:file\:org.eclipse.e4.ui.widgets_1.0.0.v20130515-1857.jar@4,reference\:file\:org.eclipse.e4.ui.workbench_1.0.2.v20131202-1739.jar@4,reference\:file\:org.eclipse.e4.ui.workbench.addons.swt_1.0.2.v20131129-1621.jar@4,reference\:file\:org.eclipse.e4.ui.workbench.renderers.swt_0.11.2.v20140205-1834.jar@4,reference\:file\:org.eclipse.e4.ui.workbench.swt_0.12.2.v20140117-1939.jar@4,reference\:file\:org.eclipse.e4.ui.workbench3_0.12.0.v20130515-1857.jar@4,reference\:file\:org.eclipse.ecf_3.2.0.v20130604-1622.jar@4,reference\:file\:org.eclipse.ecf.filetransfer_5.0.0.v20130604-1622.jar@4,reference\:file\:org.eclipse.ecf.identity_3.2.0.v20130604-1622.jar@4,reference\:file\:org.eclipse.ecf.provider.filetransfer_3.2.0.v20130604-1622.jar@4,reference\:file\:org.eclipse.ecf.provider.filetransfer.httpclient4_1.0.300.v20130604-1622.jar@4,reference\:file\:org.eclipse.ecf.provider.filetransfer.httpclient4.ssl_1.0.0.v20130604-1622.jar@4,reference\:file\:org.eclipse.ecf.provider.filetransfer.ssl_1.0.0.v20130604-1622.jar@4,reference\:file\:org.eclipse.ecf.ssl_1.1.0.v20130604-1622.jar@4,reference\:file\:org.eclipse.emf.common_2.9.2.v20131212-0545.jar@4,reference\:file\:org.eclipse.emf.ecore_2.9.2.v20131212-0545.jar@4,reference\:file\:org.eclipse.emf.ecore.change_2.9.0.v20131212-0545.jar@4,reference\:file\:org.eclipse.emf.ecore.xmi_2.9.1.v20131212-0545.jar@4,reference\:file\:org.eclipse.equinox.app_1.3.100.v20130327-1442.jar@4,reference\:file\:org.eclipse.equinox.bidi_0.10.0.v20130327-1442.jar@4,reference\:file\:org.eclipse.equinox.common_3.6.200.v20130402-1505.jar@4,reference\:file\:org.eclipse.equinox.console_1.0.100.v20130429-0953.jar@4,reference\:file\:org.eclipse.equinox.ds_1.4.101.v20130813-1853.jar@3\:start,reference\:file\:org.eclipse.equinox.event_1.3.0.v20130327-1442.jar@3\:start,reference\:file\:org.eclipse.equinox.frameworkadmin_2.0.100.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.frameworkadmin.equinox_1.0.500.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.http.jetty_3.0.100.v20130327-1442.jar@3\:start,reference\:file\:org.eclipse.equinox.http.registry_1.1.300.v20130402-1529.jar@4\:start,reference\:file\:org.eclipse.equinox.http.servlet_1.1.400.v20130418-1354.jar@3\:start,reference\:file\:org.eclipse.equinox.jsp.jasper_1.0.400.v20130327-1442.jar@4,reference\:file\:org.eclipse.equinox.jsp.jasper.registry_1.0.300.v20130327-1442.jar@4,reference\:file\:org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar@4,reference\:file\:org.eclipse.equinox.launcher.gtk.linux.x86_1.1.200.v20140116-2212/@4,reference\:file\:org.eclipse.equinox.p2.artifact.repository_1.1.200.v20130515-2028.jar@4,reference\:file\:org.eclipse.equinox.p2.console_1.0.300.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.core_2.3.0.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.director_2.3.1.v20140221-1852.jar@4,reference\:file\:org.eclipse.equinox.p2.director.app_1.0.300.v20130819-1621.jar@4,reference\:file\:org.eclipse.equinox.p2.directorywatcher_1.0.300.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.engine_2.3.0.v20130526-2122.jar@4,reference\:file\:org.eclipse.equinox.p2.extensionlocation_1.2.100.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.garbagecollector_1.0.200.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.jarprocessor_1.0.300.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.metadata_2.2.0.v20130523-1557.jar@4,reference\:file\:org.eclipse.equinox.p2.metadata.repository_1.2.100.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.operations_2.3.0.v20130711-1809.jar@4,reference\:file\:org.eclipse.equinox.p2.publisher_1.3.0.v20140129-1405.jar@4,reference\:file\:org.eclipse.equinox.p2.publisher.eclipse_1.1.200.v20130516-1953.jar@4,reference\:file\:org.eclipse.equinox.p2.ql_2.0.100.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.reconciler.dropins_1.1.200.v20130419-1850.jar@4\:start,reference\:file\:org.eclipse.equinox.p2.repository_2.3.0.v20130412-2032.jar@4,reference\:file\:org.eclipse.equinox.p2.repository.tools_2.1.0.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.touchpoint.eclipse_2.1.200.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.touchpoint.natives_1.1.100.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.transport.ecf_1.1.0.v20130516-1858.jar@4,reference\:file\:org.eclipse.equinox.p2.ui_2.3.0.v20131209-2052.jar@4,reference\:file\:org.eclipse.equinox.p2.ui.importexport_1.1.0.v20131209-2052.jar@4,reference\:file\:org.eclipse.equinox.p2.ui.sdk_1.0.300.v20130503-1750.jar@4,reference\:file\:org.eclipse.equinox.p2.ui.sdk.scheduler_1.2.0.v20140122-1840.jar@4,reference\:file\:org.eclipse.equinox.p2.updatechecker_1.1.200.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.p2.updatesite_1.0.400.v20130515-2028.jar@4,reference\:file\:org.eclipse.equinox.preferences_3.5.100.v20130422-1538.jar@4,reference\:file\:org.eclipse.equinox.registry_3.5.301.v20130717-1549.jar@4,reference\:file\:org.eclipse.equinox.security_1.2.0.v20130424-1801.jar@4,reference\:file\:org.eclipse.equinox.security.ui_1.1.100.v20130327-1442.jar@4,reference\:file\:org.eclipse.equinox.simpleconfigurator_1.0.400.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.simpleconfigurator.manipulator_2.0.0.v20130327-2119.jar@4,reference\:file\:org.eclipse.equinox.util_1.0.500.v20130404-1337.jar@4,reference\:file\:org.eclipse.help_3.6.0.v20130326-1254.jar@4,reference\:file\:org.eclipse.help.base_4.0.1.v20140221-1700.jar@4,reference\:file\:org.eclipse.help.ui_4.0.1.v20130708-1201.jar@4,reference\:file\:org.eclipse.jetty.continuation_8.1.14.v20131031.jar@4,reference\:file\:org.eclipse.jetty.http_8.1.14.v20131031.jar@4,reference\:file\:org.eclipse.jetty.io_8.1.14.v20131031.jar@4,reference\:file\:org.eclipse.jetty.security_8.1.14.v20131031.jar@4,reference\:file\:org.eclipse.jetty.server_8.1.14.v20131031.jar@4,reference\:file\:org.eclipse.jetty.servlet_8.1.14.v20131031.jar@4,reference\:file\:org.eclipse.jetty.util_8.1.14.v20131031.jar@4,reference\:file\:org.eclipse.jface_3.9.1.v20130725-1141.jar@4,reference\:file\:org.eclipse.jface.databinding_1.6.200.v20130515-1857.jar@4,reference\:file\:org.eclipse.jface.text_3.8.101.v20130802-1147.jar@4,reference\:file\:org.eclipse.jsch.core_1.1.402.v20131127-1657.jar@4,reference\:file\:org.eclipse.jsch.ui_1.1.402.v20131127-1657.jar@4,reference\:file\:org.eclipse.ltk.core.refactoring_3.6.100.v20130605-1748.jar@4,reference\:file\:org.eclipse.ltk.ui.refactoring_3.7.100.v20130605-1748.jar@4,reference\:file\:org.eclipse.osgi.services_3.3.100.v20130513-1956.jar@4,reference\:file\:org.eclipse.osgi.util_3.2.300.v20130513-1956.jar@4,reference\:file\:org.eclipse.platform_4.3.2.v20140221-1700/@4,reference\:file\:org.eclipse.rcp_4.3.0.v20140221-1700.jar@4,reference\:file\:org.eclipse.search_3.9.1.v20131121-1346.jar@4,reference\:file\:org.eclipse.swt_3.102.1.v20140206-1334.jar@4,reference\:file\:org.eclipse.swt.gtk.linux.x86_3.102.1.v20140206-1358.jar@4,reference\:file\:org.eclipse.team.core_3.7.0.v20130514-1224.jar@4,reference\:file\:org.eclipse.team.ui_3.7.1.v20130729-1104.jar@4,reference\:file\:org.eclipse.text_3.5.300.v20130515-1451.jar@4,reference\:file\:org.eclipse.ui_3.105.0.v20130522-1122.jar@4,reference\:file\:org.eclipse.ui.browser_3.4.100.v20130527-1656.jar@4,reference\:file\:org.eclipse.ui.cheatsheets_3.4.200.v20130326-1254.jar@4,reference\:file\:org.eclipse.ui.console_3.5.200.v20130514-0954.jar@4,reference\:file\:org.eclipse.ui.editors_3.8.100.v20130513-1637.jar@4,reference\:file\:org.eclipse.ui.externaltools_3.2.200.v20130508-2007.jar@4,reference\:file\:org.eclipse.ui.forms_3.6.1.v20130822-1117.jar@4,reference\:file\:org.eclipse.ui.ide_3.9.2.v20131004-0923.jar@4,reference\:file\:org.eclipse.ui.ide.application_1.0.401.v20131105-1007.jar@4,reference\:file\:org.eclipse.ui.intro_3.4.200.v20130326-1254.jar@4,reference\:file\:org.eclipse.ui.intro.universal_3.2.600.v20130326-1254/@4,reference\:file\:org.eclipse.ui.navigator_3.5.300.v20130517-0139.jar@4,reference\:file\:org.eclipse.ui.navigator.resources_3.4.501.v20131021-1545.jar@4,reference\:file\:org.eclipse.ui.views_3.6.100.v20130326-1250.jar@4,reference\:file\:org.eclipse.ui.views.properties.tabbed_3.6.0.v20130515-1857.jar@4,reference\:file\:org.eclipse.ui.workbench_3.105.2.v20140211-1711.jar@4,reference\:file\:org.eclipse.ui.workbench.texteditor_3.8.101.v20130729-1318.jar@4,reference\:file\:org.eclipse.update.configurator_3.3.200.v20140203-1328.jar@4,reference\:file\:org.sat4j.core_2.3.5.v201308161310.jar@4,reference\:file\:org.sat4j.pb_2.3.5.v201308161310.jar@4,reference\:file\:org.w3c.css.sac_1.3.1.v200903091627.jar@4,reference\:file\:org.w3c.dom.smil_1.0.0.v200806040011.jar@4,reference\:file\:org.w3c.dom.svg_1.1.0.v201011041433.jar@4,reference\:file\:com.ibm.icu.base_50.1.1.v201304230130.jar@4,reference\:file\:../dropins/plugins/com.ibm.retail.xpd.pos.messaging_1.0.0.201408060721/@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.activemq_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.activemq.broker.setup_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.activemq.provider_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.bundle.auto.start_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.common_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.common.metriclogger_1.0.0.201408060721/@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.jetty.setup_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.jms.provider.extension_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.message.broker.service_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.pos.messaging_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.rsm_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.rsm.web_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.server_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.si.deps_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.webserver.services_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.webserver.staticdir_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.websphere.mq.provider_7.0.1.8_1.0.0.201408060721.jar@4,reference\:file\:../dropins/plugins/org.apache.mina_2.0.7.201408060721.jar@4,reference\:file\:../dropins/plugins/org.apache.wink_1.4.0.201408060721.jar@4,reference\:file\:../dropins/plugins/org.slf4j_1.7.6.201408060721.jar@4,reference\:file\:../dropins/plugins/com.tgcs.scrt.walmart.server.branding_1.0.0.201408122025.jar@4

3 个答案:

答案 0 :(得分:2)

从文件中的所有行中删除所有字符串:/start的sed命令将是

sed -e 's/:\/start//g'

但由于您的数据包含:start,您可能必须使用

sed -e 's/:start//g

而第三种选择(叹气)是

sed -e 's/\\:start//g

对于特定字符串,您可以执行

-e 's/\(reference\\:file\\:org.eclipse.equinox.http.registry_1.1.300.v20130402-1529.jar@4\)\\:start/\1/'

并在命令行上为每个字符串重复此选项。详细说明:

-e 's/\(the-string-identifying-the-context\)\\:start/\1/'

确保在-string-identify-the-context中复制\。请注意在替换字符串中反向引用的捕获组。

答案 1 :(得分:0)

sed -e 's/\\:start$//'

示例运行:

%  echo "reference\:file\:org.eclipse.equinox.http.servlet_1.1.400.v20130418-1354.jar@3\:start" | sed -e 's/\\:start$//'
reference\:file\:org.eclipse.equinox.http.servlet_1.1.400.v20130418-1354.jar@3

'$'仅在该行的末尾匹配。希望有所帮助...

答案 2 :(得分:0)

使用反向引用,例如

sed -i -e 's/\(org.eclipse.equinox.http.registry_1.1.300.v20130402-1529.jar@4\)\\:start/\1/' yourFileName

阅读sed users manual

相关问题