【问题标题】:Shell Script to remove lines of text in a text file用于删除文本文件中的文本行的 Shell 脚本
【发布时间】:2014-10-23 15:00:10
【问题描述】:

如果文件中存在一行文本,如何删除它?

目前为止我猜

#!/sbin/sh

mount -o remount,rw /system;

# Make a backup first
cp /system/build.prop /system/build.prop.bak;

# Append
if [ grep -o 'wifi.supplicant_scan_interval' <<</system.build.prop > 1 ]; then
    echo "YO";
fi;

mount -o remount,ro /system;

但是,这向我显示了YO,无论它是> 1 还是

你能帮忙吗?

代码更新

#!/sbin/sh
mount -o remount,rw /system;

function check_prop(){
    busybox grep $1 /system/build.prop;
    return $?;
}

# Make a backup first
cp /system/build.prop /system/build.prop.bak;

echo $(check_prop 'wifi.supplicant_scan_interval');

# Append
if [ $(check_prop 'wifi.supplicant_scan_interval') > 1 ]; then
    # Do my stuff here?
    echo 'YO';
fi;

mount -o remount,ro /system;

给我一​​个空行,YO。如果我将其更改为

【问题讨论】:

  • 如果您尝试 grep 文件 /system/build.prop(请注意问题中的拼写错误),则不应使用 &lt;&lt;&lt; 语法。

标签: android bash shell sh


【解决方案1】:
sed '/wifi.supplicant_scan_interval/d' inputfile 

将删除匹配wifi.supplicant_scan_interval的行

例如

$cat input 
hello
world 
hai
$sed '/world/d' input 
hello
hai

如果您想从文件中删除行,请使用 -i 选项,该选项执行操作 inplace

sed -i '/wifi.supplicant_scan_interval/d' inputfile 

编辑

使用grep 打印除匹配模式的行之外的所有行。

grep -v 'wifi.supplicant_scan_interval' inputfile 

例如

$ grep -v 'world' input 
hello
hai

-v 选项进行否定。

【讨论】:

  • 但是 sed 在 android 上的 busybox 中可用吗? (是的,显然是......)但我还需要检查它是否存在 1st,因为除了删除该行之外,我还需要做其他事情
  • @Kevin 你也可以使用grep 查看编辑。希望它有所帮助:)
  • 是的。看到了,但我仍然需要先检查它是否存在
  • 我还没有真正有机会查看和测试,所以请原谅我现在...这(grep)是删除该行,还是只删除它找到的文本?
  • @Kevin 从输出中删除整行,但不编辑文件
猜你喜欢
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 2022-07-11
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 1970-01-01
相关资源
最近更新 更多