Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除小于前面值的数值_Python_Linux_Ubuntu_Sed_Grep - Fatal编程技术网

Python 删除小于前面值的数值

Python 删除小于前面值的数值,python,linux,ubuntu,sed,grep,Python,Linux,Ubuntu,Sed,Grep,我有以下格式的数据: 1 "there" ... 23489 "abc" 23490 "hello" 23491 "hi" 2 "def" 23492 "stackoverflow" 1 "there" ... 23489 "abc" 23490 "hello" 23491 "hi" 23492

我有以下格式的数据:

      1      "there"
      ...
      23489  "abc"
      23490  "hello"
      23491  "hi"
      2      "def"
      23492  "stackoverflow"
      1      "there"
      ...
      23489  "abc"
      23490  "hello"
      23491  "hi"
      23492  "stackoverflow"
      1      "the"  
      ...
      23489  "abc"
      23490  "hel"
      23491  "hi"
      23492  "sta"

我想删除从小于前面数值的数值开始的所有行(即,我想删除:2“def”,因为2您可能可以使它与此脚本一起工作(未测试,但可能需要更改)


使用
awk
你可以说:

awk '{if ($1<prev){next}}{prev=$1}1' inputfile
awk '{if ($1<prev){next}}{prev=$1; gsub("\"", "", $2); $2=substr($2,0,3);$2="\""$2"\""}1' inputfile

是的,这是一个更甜蜜的解决方案:)@devnull非常感谢你的帮助。如果可能的话,你能解释一下吗。。please@AliceEverett添加了上述解释。@devnull是否也可以在同一命令中将字符串长度限制为3。。我知道这是一个有点独立的问题。但由于您的解决方案非常优雅。因此,我想结合长度使用用户解决方案restriction@AliceEverett你说的绳子的长度是什么意思?输入中第一列的长度?
$ awk '{if ($1<prev){next}}{prev=$1}1' inputfile
1      "there"
23489  "abc"
23490  "hello"
23491  "hi"
23492  "stackoverflow"
awk '{if ($1<prev){next}}{prev=$1; gsub("\"", "", $2); $2=substr($2,0,3);$2="\""$2"\""}1' inputfile