1

I'm looking to remove (from a file) all lines which match with a number which is at the 34th position with the following command :

sed -n "/^.{33}16184198166000003.*$/!p" inFile >> outFile

It's working with the following command (it's pretty bad I know):

sed -n "/^.................................16184198166000003.*$/p" inFile >> outFile

I can't understand why it isn't working in both cases.

This is some lines from my file:

aaaaaaaaaaasfqfqfqsfqfsqfsqaaaaaa16184198166000003qsdfqesrfzqssdgsdqsfsf  zerzer z ez r
sF SDF EZ   EZ FF F ZE  EFedff<df16184198166000003sssssssssssssssssssssssssssssssssssss

The number to find:

16184198166000003

The number is 17th char long and must be at the 34th position. Each line represent an accounting bill and this number can be present in another position (for example at the 113th position). In this case I must not delete the line.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
pops
  • 15
  • 3

3 Answers3

2

With .{33} you are using curly braces as a regular expression. The default sed does not interpret them, so you need to use -r to enable extended regular expressions:

sed -rn "/^.{33}16184198166000003.*$/d" file

From man sed:

-r, --regexp-extended

use extended regular expressions in the script.

This way, the {} regular expression is interpreted properly.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Thanks but That doesn't work for me : sed: illegal option -- r, Usage: sed [-n] [-u] Script [File ...] sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...] – pops Jul 20 '15 at 06:07
  • [link] http://stackoverflow.com/questions/7232797/sed-on-aix-does-not-recognize-i-flag – pops Jul 20 '15 at 06:22
  • Then you should go for any of the other solutions, that do escape the regex instead of using `-r`. – fedorqui Jul 20 '15 at 20:51
1

I think you're missing the escape sequence on brackets. This worked for me:

sed  "/^.\{33\}16184198166000003.*$/d" input.txt > output.txt

Tested on ubuntu 14.04, bash 4.3.11, sed 4.2.2.

If above answer does not work for you, consider using groups:

sed  "/^.\(\{33\}\)16184198166000003.*$/d" input.txt > output.txt
brunorey
  • 2,135
  • 1
  • 18
  • 26
0

You can use this sed to make it fast:

sed -i.bak '/^.\{33\}16184198166000003/d' file

This will find 16184198166000003 starting from 34th position and if matches then it will delete those lines.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • As @fedorqui it doesn't work for me : `sed: illegal option -- i Usage: sed [-n] [-u] Script [File ...] sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]` [link] http://stackoverflow.com/questions/7232797/sed-on-aix-does-not-recognize-i-flag – pops Jul 20 '15 at 06:17
  • What is your sed version. This worked for me even on BSD sed – anubhava Jul 20 '15 at 06:26
  • Try without `-i` i.e. `sed /^.\{33\}16184198166000003/d' file` – anubhava Jul 20 '15 at 06:26
  • I'm on AIX v3r5 (uname -r -v), I can't find my ksh version (echo $KSH_VERSION doesn't works) and sed version (sed --version or sed -v are not working). AIX doc : http://www-01.ibm.com/support/knowledgecenter/ssw_aix_53/com.ibm.aix.cmds/doc/aixcmds5/sed.htm?cp=ssw_aix_53%2F1-2-0-18-22 – pops Jul 20 '15 at 07:02
  • Did you try: `sed /^.\{33\}16184198166000003/d' file` command? – anubhava Jul 20 '15 at 10:09