Question on sed command?
I am trying to use sed to delete parts of a text file with lines like:
23920 E S:1 R:2 C:14 L:开发者_开发知识库5 ch 80 7279 1113 5272 -342 1168 5642 -347 1265 5587
23921 E S:1 R:2 C:14 L:6 ch 1 4605 1267 4586 11 1331 4587 -31 1306 4692
The parts I need to delete are the parts like E S:1 R:2 C:14 L:5 ch 80
and E S:1 R:2 C:14 L:6 ch 1
in every line. The numbers change throughout the file, but are always between 1 and 100.
You could also use cut
for this, if it always the same fields:
jed@jed-osx:~$ echo "23920 E S:1 R:2 C:14 L:5 ch 80 7279 1113 5272 -342 1168 5642 -347 1265 5587
23921 E S:1 R:2 C:14 L:6 ch 1 4605 1267 4586 11 1331 4587 -31 1306 4692" | cut -d" " -f1,8-
23920 80 7279 1113 5272 -342 1168 5642 -347 1265 5587
23921 1 4605 1267 4586 11 1331 4587 -31 1306 4692
EDIT: Explanation of the cut command used:
-d" "
Use space as the delimiter
-f 1,8-
Return field 1, field 8, and all fields after 8
A sed solution
linux-t77m:$ more st.txt
23920 E S:1 R:2 C:14 L:5 ch 80 7279 1113 5272 -342 1168 5642 -347 1265 5587
23921 E S:1 R:2 C:14 L:6 ch 1 4605 1267 4586 11 1331 4587 -31 1306 4692
linux-t77m:$ sed -r "s/E S:.* ch [0-9]+ //g" st.txt
23920 7279 1113 5272 -342 1168 5642 -347 1265 5587
23921 4605 1267 4586 11 1331 4587 -31 1306 4692
This is done with a regular expression substitution. The command s/<regexp>/<substitution>/g replaces every part of every line matching <regexp> for <substitution>.
In this case <regexp> is E S:.* ch [0-9]+
which means:
- search for E S:
- then seach for everything until you see
- a space preceding ch followed by a space, one or more digits and another space
And <substitution> is the empty string, so it effectively deletes the parts of the lines matching it.
The -r switch signals sed we are using an 'extended' regular expressions, which are usually clearer because they don't need so many backslashes which standard sed regexps would require.
精彩评论