在shell脚本中拆分unix文件
我有一个包含以下内容的文件xyz1.sh,当我执行cat xyz1时,我会得到结果:在shell脚本中拆分unix文件,unix,Unix,我有一个包含以下内容的文件xyz1.sh,当我执行cat xyz1时,我会得到结果: `$echo "Welcome to the server" Welcome to the server $echo "Hi this is content in the test file1." Hi this is content in the test file1. $ls abc.txt $date 17 October 2016` 我的目标是只使用shell命令生成一个新文件xyz2。
`$echo "Welcome to the server"
Welcome to the server
$echo "Hi this is content in the test file1."
Hi this is content in the test file1.
$ls
abc.txt
$date
17 October 2016`
我的目标是只使用shell命令生成一个新文件xyz2。除了在xyz1中搜索某些特定的命令集之外,还有什么有效的方法来实现这一点吗。是否有任何方法可以识别所有unix命令并从文件中取出行
xyz2具有以下内容:
`$echo "Welcome to the server"
$echo "Hi this is content in the test file1."
$ls
$date`
TIA您可以使用Unix
sed
命令来处理文件。下面的命令
d删除与给定正则表达式匹配的每一行,/^[^$]/
(它会查找与$
不匹配的每一行)。然后重定向输出以创建新文件xyz2
sed '/^[^$]/d' xyz1.sh > xyz2
谢谢但这是一个示例代码。在每行之前可能有主机名前面的文件,例如user@hostname:>cd dir1so在本例中,正在寻找能够将其识别为命令的东西,这将是一个比发布的问题更复杂的问题。您应该将其作为一个单独的问题发布,提供一个/示例文件,包括所有可能的输入。