sed on Bash字符串变量

时间:2017-03-15 18:49:12

标签: bash sed

我有一个变量:

temp='Some text \n Some words \n'

我想用sed删除其中一些行:

sed -e '1d' $temp 

我想知道这是否可行。

2 个答案:

答案 0 :(得分:4)

当您将字符串作为参数传递时,sed会将其解释为文件名或文件名列表:

sed -e '1d' "$temp"

当然,这不是你想要的。

您需要在此处使用字符串<<<

temp=$'Some text\nSome words\nLast word'
sed '1d' <<< "$temp"

输出:

Some words
Last word

答案 1 :(得分:0)

与posix兼容的方法是:

InputStream is = MyStaticClass.class.getResourceAsStream("/ressources/" + "file.File1")
Scanner scanner = new Scanner(new InputStreamReader(is, "utf-8"));
try {
    if (scanner.hasNextLine()) {
        System.out.println(scanner.nextLine());
    }
} finally {
    scanner.close();
}

如果您只需要与bash兼容的解决方案,请参阅codeforester's answer,因为此处的字符串语法更易于阅读。