使用sed替换未知字符串的一部分

时间:2019-03-04 01:57:01

标签: regex bash sed

我有一个文本文件:

example.txt

UserName = JIU62H123;
USER_NAME = JIU62H123;

我需要将JIU62H123更改为A5B4C6DF9。显然,对于此文件,我可以这样做:

//bash
sed -i '' 's/JIU62H123/A5B4C6DF9/g' example.txt

此结果适用于此文件,如您从结果中看到的那样:

example.txt

UserName = A5B4C6DF9;
USER_NAME = A5B4C6DF9;

但是,实际上,我不知道用户名是否真的提前。我所知道的是文件的形状是这样的:

example.txt

UserName = <some_letters_and_numbers>;
USER_NAME = <some_letters_and_numbers>;

我基本上需要将<some_letters_and_numbers>更改为A5B4C6DF9。可以使用一个或多个sed命令来完成此操作吗?

5 个答案:

答案 0 :(得分:3)

使用正则表达式如何?

sed -ir 's/^(USER_NAME|UserName) = [A-Za-z0-9]+;$/\1 = A5B4C6DF9;/g' example.txt

答案 1 :(得分:2)

真实的话:

  • UserNameUSER_NAME开头的行,后跟等号=
  • 用...代替右手

sed语法:

使用变量来提高适应性

newId=A5B4C6DF9
sed -e '/^U\(serName\|\SER_NAME) *=/s/= .*$/'$newId/ -i example.txt

Nota (带引号的部分在变量之前!)

这项工作直到$newId都不包含特殊字符。如果变量可以包含空格或其他非字母数字字符,请使用双引号:

sed -e '/^U\(serName\|\SER_NAME) *=/s/= .*$/'"$newId"/ -i example.txt

答案 2 :(得分:1)

使用awk,您可以:

$ awk '{$3="A5B4C6DF9;"}1' file
UserName = A5B4C6DF9;
USER_NAME = A5B4C6DF9;

即。替换第三个空格分隔的字段。使用recent enough GNU awk,您可以进行就地编辑:

$ awk -i inplace '{$3="A5B4C6DF9;"}1' file

答案 3 :(得分:1)

对于def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' } def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' } apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion 27 lintOptions { disable 'InvalidPackage' } defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.******.********" minSdkVersion 16 targetSdkVersion 27 versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { // TODO: Add your own signing config for the release build. // Signing with the debug keys for now, so `flutter run --release` works. signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } apply plugin: 'com.google.gms.google-services' ,并假设您要向change <some_letters_and_numbers> to A5B4C6DF9添加具有ERE的on the right side of "= "的sed,则为:

-E

以及任何POSIX sed:

$ sed -E 's/= [[:alnum:]]+/= A5B4C6DF9/' file
UserName = A5B4C6DF9;
USER_NAME = A5B4C6DF9;

答案 4 :(得分:0)

非常感谢您的回答,并为每个答案都表示赞赏。我最终做了这样的解决方案:

sed -i '' -E 's/(DEVELOPMENT_TEAM|DevelopmentTeam)( = )(.*)/\1\2A5B4C6DF9;/' example.txt

此处给出的大多数答案似乎都与字母/数字模式匹配,但我认为最好将其与任何内容匹配并用所需的内容替换。