tags:

views:

54

answers:

2

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.

+2  A: 

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

Jed Daniels
+1  A: 

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:

  1. search for E S:
  2. then seach for everything until you see
  3. 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.

Vinko Vrsalovic
Can you explain what each part of the sed command above does? This way we can all learn instead of relying on sed masters like you for each variation. I have to admit I was guilty of not explaining my cut command, so I edited and added the explanation, but sed is much more complicated.
Jed Daniels
@Jed: There you go
Vinko Vrsalovic