Hi guys
I am deleting a unique line from the file and also need to remove the line above it which is NOT unique and servers as a record separator. Here is an example:
#
101 803E 823F 8240
#
102 755f 4F2A 4F2B
#
290 747D 0926 0927
#
999 8123 813E 8155
#
206 807C 803F 8237
Deleting a unique line is not a problem, but I cannot figure out how to remove the line above it (starts with # ...)
May be someone already handle similar problem.
Thanks a lot for advice
For this, sed is a nice tool:
sed '
:loop
$b
N
/\nYOUR_PATTERN/d
P
s/.*\n//
b loop
' infile >outfile
If at end of file, branch to <no_tag>, which is to end of script, print and exit (some sed are funny with N at EOF). Get a second line into the buffer. If YOUR_PATTERN is the second line, delete both. Otherwise, spit out the first line, delete it and go back to 'loop' to get another line.
In vi, this is an ex ':' global command:
:g/YOUR_PATTERN/.-1,.d
DGPickett,
thanks for advice, but for some reason it down not remove the requested lines
This is before the change:
$ grep " 2001 8232 8232 8232" firmexbytes.dta.new
2001 8232 8232 8232
$
Here is the script:
$ sed '
> :loop
> $b
> N
> /\n"2001 8232 8232 8232"/d
> P
> s/.*\n//
> b loop
> ' firmexbytes.dta.new > firmexbytes.dta.del
This is after the script was run:
$ grep " 2001 8232 8232 8232" firmexbytes.dta.del
2001 8232 8232 8232
$
You missed some quoting,m and now are looking for it and the double quotes (literally because you are in single quotes). I do sed scripts inline in single quotes, but temporrily close the single quoting and go to double to access a shell provide env var, then close the double and restart the single quoting. If you put in only literals, the outer single quotes are sufficient.
Second, the pattern must have any leading white space characters after \n.
You can also try:
tac file|awk '/PATTERN/{getline;next}' |tac
I wonder what the overhead is for tac? VM, seek, mmap()? Since it works on pipes, it is probably VM intensive! Solaris has "tail -r".