使用sed脚本

时间:2015-10-16 03:44:45

标签: linux bash sed

我想使用sed通过其值扩展/替换变量。例 文件 TestVarExpand.txt 包含

Student: $name
Class..: ${classname}

在命令行中,手动输入:

v1="John McGyver"
v2="Molecular Biology CS234"
sed -re "s/[$]name/$v1/" -e "s/[$]\{classname\}/$v2/g" TestVarExpand.txt

控制台输出(正确):

  

学生:John McGyver
  分类:分子生物学CS234

在真实场景中,我有很多变量需要扩展。所以我想到使用Sed - An Introduction and Tutorial by Bruce Barnett中教导的sed脚本: 内容 TestVarExpand.sed

#!/bin/sed -rf

s|[$]name|"$v1"|g
s|[$]\{classname\}|'$v2'|g

通过以下方式测试:

chmod 744 ./TestVarExpand.sed
v1="John McGyver"
v2="Molecular Biology CS234"
./TestVarExpand.sed < ./TestVarExpand.txt

控制台输出(不正确):

  

学生:“$ v1”
  类..:'$ v2'

问题:TestVarExpand.sed中的语法是什么,在命令行中使用sed -re“expr”等效(双引号来评估变量)?感谢。

1 个答案:

答案 0 :(得分:0)

需要并导出以在脚本中使用变量,并且您可以在bash中使用sed以在这种情况下使用其他shell操作。

TestVarExpand.sh

 java.lang.ClassCastException:    keleno.example.ramz.mapper_city.MainActivity@21c6ea2b must implement   OnFragmentInteractionListener
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:155)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5343)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:  Caused by: java.lang.ClassCastException: keleno.example.ramz.mapper_city.MainActivity@21c6ea2b must implement OnFragmentInteractionListener
10-16 11:28:13.664 28882-28882/keleno.example.ramz.mapper_city E/AndroidRuntime:     at keleno.example.ramz.mapper_city.fragment.ContactFragment.onAttach(ContactFragment.java:85)

带数据

#!/bin/bash

sed -e "
   s/[$]name/${v1}/g
   s/[$]{classname}/${v2}/g
   " $1

结果

chmod 744 ./TestVarExpand.sh
export v1="John McGyver"
export v2="Molecular Biology CS234"
./TestVarExpand.sh ./TestVarExpand.txt