每行以&#34开头;文件:"和"修改:"
我需要从第二次出现"修改" "修改"块中的模式线。
保持第一次出现"修改"在每个"修改块"
基于此文
File: /Music/David Bowe/1.mp3
Modify: 2010-11-24 09:36:33.000000000 -0200
Modify: 2016-04-13 11:32:58.692458900 -0300
File: /Music/Music/David Bowe/Tom.mp3
Modify: 2010-10-22 11:12:15.000000000 -0200
File: /Music/Music/David Bowe/Hero.mp3
Modify: 2010-10-26 07:21:56.000000000 -0200
Modify: 2017-04-13 11:32:58.282458400 -0300
File: /Music/Music/David Bowe/AbsoluteBeginners.mp3
Modify: 2010-10-22 11:12:07.000000000 -0200
File: /Music/Music/David Bowe/ImAfraidOfAmericans.mp3
Modify: 2011-11-13 08:54:57.000000000 -0200
Modify: 2015-04-13 11:32:58.732459000 -0300
Modify: 2016-04-13 11:32:58.692458900 -0300
Modify: 2016-01-01 21:18:53.348891500 -0200
我想得到这个结果
File: /Music/David Bowe/1.mp3
Modify: 2010-11-24 09:36:33.000000000 -0200
File: /Music/Music/David Bowe/Tom.mp3
Modify: 2010-10-22 11:12:15.000000000 -0200
File: /Music/Music/David Bowe/Hero.mp3
Modify: 2010-10-26 07:21:56.000000000 -0200
File: /Music/Music/David Bowe/AbsoluteBeginners.mp3
Modify: 2010-10-22 11:12:07.000000000 -0200
File: /Music/Music/David Bowe/ImAfraidOfAmericans.mp3
Modify: 2011-11-13 08:54:57.000000000 -0200
sed '/^Modify:/d' sample.txt
无法正常工作,因为我删除了该模式的所有参数。
sed '/^Modify:/2,$d' sample.txt
尝试从第二次到最后一次删除时不再起作用(语法错误)
答案 0 :(得分:1)
使用GNU sed:
<!-- Read a csv file -->
<property name="resource" value="file:#{jobParameters[bmsCSVFileInput]}" />
<!-- Skip the header line -->
<property name="linesToSkip" value="1" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<!-- split it -->
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="PROJ_TYP_CD, ...removed ... REFRESH_TMS" />
</bean>
</property>
<property name="fieldSetMapper">
<bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="bmsContract" />
</bean>
</property>
</bean>
</property>
或更短:
sed -n '/File:/N; /File:/p' file
输出:
File: /Music/David Bowe/1.mp3 Modify: 2010-11-24 09:36:33.000000000 -0200 File: /Music/Music/David Bowe/Tom.mp3 Modify: 2010-10-22 11:12:15.000000000 -0200 File: /Music/Music/David Bowe/Hero.mp3 Modify: 2010-10-26 07:21:56.000000000 -0200 File: /Music/Music/David Bowe/AbsoluteBeginners.mp3 Modify: 2010-10-22 11:12:07.000000000 -0200 File: /Music/Music/David Bowe/ImAfraidOfAmericans.mp3 Modify: 2011-11-13 08:54:57.000000000 -0200
来自sed -n '/File:/{N;p}' file
:
选项:
man sed
:禁止自动打印图案空间
命令:
-n
:将下一行输入附加到模式空间
N
:打印当前的模式空间。
答案 1 :(得分:1)
这可能适合你(GNU sed):
sed -n '/File/,/Modify/p' file
关闭seds普通print-every-line并仅在指定时打印。打印File
和Modify
之间的行范围,不会打印所有其他行。
更强大的解决方案:
sed -n '/^ File:/{N;/^Modify:/M!D;p}' file
使用-n
类似grep的选项。匹配特定的File:
行,附加下一行并匹配特定的Modify:
行,如果没有匹配则删除第一行并重复,否则打印两行并重复。
N.B。删除第一行时,不遵循seds正常处理。因为模式空间不是空的,所以不会自动读取下一行,并且命令循环开始就好像它已经被读取一样,即附加了N
命令的第二行,现在成为模式中的第一行空格和要执行的第一条指令是/^ File:/
。
答案 2 :(得分:0)
您可以使用grep:
$ cat > trash.txt
File: /Music/David Bowe/1.mp3
Modify: 2010-11-24 09:36:33.000000000 -0200
Modify: 2016-04-13 11:32:58.692458900 -0300
File: /Music/Music/David Bowe/Tom.mp3
Modify: 2010-10-22 11:12:15.000000000 -0200
File: /Music/Music/David Bowe/Hero.mp3
Modify: 2010-10-26 07:21:56.000000000 -0200
Modify: 2017-04-13 11:32:58.282458400 -0300
File: /Music/Music/David Bowe/AbsoluteBeginners.mp3
Modify: 2010-10-22 11:12:07.000000000 -0200
File: /Music/Music/David Bowe/ImAfraidOfAmericans.mp3
Modify: 2011-11-13 08:54:57.000000000 -0200
Modify: 2015-04-13 11:32:58.732459000 -0300
Modify: 2016-04-13 11:32:58.692458900 -0300
Modify: 2016-01-01 21:18:53.348891500 -0200
^D
$ grep File -A 1 trash.txt | grep -v -- --
File: /Music/David Bowe/1.mp3
Modify: 2010-11-24 09:36:33.000000000 -0200
File: /Music/Music/David Bowe/Tom.mp3
Modify: 2010-10-22 11:12:15.000000000 -0200
File: /Music/Music/David Bowe/Hero.mp3
Modify: 2010-10-26 07:21:56.000000000 -0200
File: /Music/Music/David Bowe/AbsoluteBeginners.mp3
Modify: 2010-10-22 11:12:07.000000000 -0200
File: /Music/Music/David Bowe/ImAfraidOfAmericans.mp3
Modify: 2011-11-13 08:54:57.000000000 -0200
说明:
-A 1
表示匹配后的1行上下文(带有文件的行:) | grep -v -- --
这个是删除带有--
的行(-A标志放置一个--
行以标记组分隔符)所以我们需要第二个grep:{{1 }表示“仅匹配没有此模式的行”,我们第一个-v
只是转义第二个--
,因为--
在大多数gnu命令行实用程序中具有特殊含义