Find and replace all using regex - regex

It would be greatly appreciated if someone could help.
I need to append wrapped lines to where it used to belong.
INPUT:
HOST 00 0 00 00 0 9999 BBB_AAA 0 23 03 (RAM) (U)
(PKK) (LNA) (HLD) $
HOST 00 0 00 00 0 $
HOST 00 0 00 00 1 9999 AAA_AAA 0 32 03 (RAM) (U)
(PKK) (LNA) (HLD) $
REQUIRED OUTPUT:
HOST 00 0 00 00 0 9999 BBB_AAA 0 23 03 (RAM) (U) (PKK) (LNA) (HLD) $
HOST 00 0 00 00 0 $
HOST 00 0 00 00 1 9999 AAA_AAA 0 32 03 (RAM) (U) (PKK) (LNA) (HLD) $
I use Reg Expression
Find what : ^\(.*

Find what: (\(U\))\s+(\(PKK\))
Replace with: $1 $2

How about:
Find what: (?<!\$)\n
Replace with: nothing

Related

how to find angle braket literals with grep

In the following example, the second match "<_dl_start_user>" was unexpected:
$ objdump -D /lib64/ld-linux-x86-64.so.2|grep -A5 '<_start>:'
0000003ba0400b30 <_start>:
3ba0400b30: 48 89 e7 mov %rsp,%rdi
3ba0400b33: e8 28 06 00 00 callq 3ba0401160 <_dl_start>
0000003ba0400b38 <_dl_start_user>:
3ba0400b38: 49 89 c4 mov %rax,%r12
how can I match exactly '<_start>:' ?
You are matching <_start>: exactly. You're also seeing 5 lines of trailing context after the match because you specified -A5.

grep -v pattern and also remove 1 line before and 4 lines after [duplicate]

This question already has answers here:
How to exclude several lines around match with grep or similar tool?
(2 answers)
Closed 8 years ago.
I would like to grep a pattern and remove the line of the matching pattern and also 1 line before and 4 lines after the context. I tried:
grep -v -A 4 -B 1
Thanks in advance!
Example:
Rule: r1
Owner: Process explorer.exe Pid 1544
0x01ec350f 8b 45 a8 0f b6 00 8d 4d a8 ff 14 85 c8 7f ed 01 .E.....M........
0x01ec351f 84 c0 75 ec 8b 4d fc e8 ba f5 fe ff f7 85 b0 fd ..u..M..........
0x01ec352f ff ff 00 00 01 00 75 13 33 c0 50 50 50 68 48 28 ......u.3.PPPhH(
0x01ec353f eb 01 33 d2 8b cb e8 b0 57 ff ff f7 05 8c 9b ed ..3.....W.......
I would like to grep "explorer.exe" and remove the line and also 1 line before and 4 lines after.
awk
this awk one-liner would help:
awk 'NR==FNR{if(/explorer[.]exe/)d[++i]=NR;next}
{for(x=1;x<=i;x++)if(FNR>=d[x]-1&&FNR<=d[x]+4)next}7' file file
see this example:
kent$ cat f
foo
foo2
Rule: r1
Owner: Process explorer.exe Pid 1544
remove1
remove2
remove3
remove4
bar
bar2
kent$ awk 'NR==FNR{if(/explorer[.]exe/)d[++i]=NR;next}{for(x=1;x<=i;x++)if(FNR>=d[x]-1&&FNR<=d[x]+4)next}7' f f
foo
foo2
bar
bar2
vim
if vim is also possible for you, it could be a lot easier:
:g/Pattern/norm! k6dd
Note, the vim solution would have problem in first match if your pattern was on the 1st line in your file.

Finding a integer number after a beginning t=

I have a string like this:
33 00 4b 46 ff ff 03 10 30 t=25562
I am only interested in the five digits at the very end after the t=
How can I get this numbers with a regular expression out of it?
I tried grep t=..... but I also got all characters including the t= in the beginning, which I would like to drop?
After finding that five digit number, I would like to divide this by 1000. So in the above mentioned case the number 25.562. Is this possible with grep and regular expressions?
Thanks for your help.
Using awk
echo '33 00 4b 46 ff ff 03 10 30 t=25562' | awk -F= '{print $2/1000}'
Output:
25.562
EDIT
As pointed out by #anubhava in comment, above assumes = is not present anywhere before t=. If that's not the case,
echo '33 00 4b 46 ff ff 03 10 30 t=25562' | awk -F' t=' '{print $2/1000}'
This should be OK. It will only get value form t= at the end. OP also post that t= could exist as extra character in the middle of the line, but hi only like to get the one at the end of the line.
echo '33 00 4b 46 ff ff t=22 03 10 30 t=25562' | awk '{split($NF,a,"=");print a[2]/1000}'
25.562
Another variation
echo '33 00 4b 46 ff ff t=22 03 10 30 t=25562' | awk 'END {print $0/1000}' RS==
25.562

Parsing memory dump (looking for a more elegant solution)

I am trying to parse the output of the s51 simulator using a bash script and sed. In a first step I want one single string containing all the bytes in hexadecimal form. The output of the simulator looks like this. The real output can be much longer, up to 64k.
0x0000 10 11 12 13 14 15 16 17 ........
0x0008 18 19 00 00 00 00 00 00 ........
0x0010 00 00 00 00 00 00 00 00 ........
0x0018 00 00 00 00 00 00 00 00 ........
0x0020 00 00 00 00 00 00 00 00 ........
0x0028 00 00 00 00 00 00 00 00 ........
0x0030 00 00 00 00 00 00 00 00 ........
timer #0("time") ON: 0.001085 sec (13020 clks)
timer #0("isr") ON,ISR: 0 sec (0 clks)
timer #0("idle") ON,ISR: 0 sec (0 clks)
My code to parse is the following:
sed -e ':loop' -e 's/\s\([0-9a-f]\{1\}\)\([0-9a-f]\{1\}\)/\2\1/g' -e 't loop' -n -e 's/.*\(0x[0-9a-f]\{4\}\)\([0-9a-f]\{16\}\).*/\2/p' | sed -e ':a;N;$!ba;s/\n//g'
The first 3 parts swap the two digits of each byte and removes the space. The 4th part removes the other lines and the address and ascii representation. The last part removes the connects the lines.
This outputs a string likes this:
01112131415161718190000000....
I was wondering what I could do better.
This might work for you (GNU sed):
sed '/^0x\S\{4\}\(\( \S\S\)\{8\}\).*/{s//\1/;H};$!d;x;s/\n//g;s/ \(.\)\(.\)/\2\1/g' file
or (at a pinch):
sed -r '/^0x....(( ..){8}).*/{s//\1/;H};$!d;x;s/\n//g;s/ (.)(.)/\2\1/g' file
I think the following should be equivalent:
sed -n -e '/^0x[0-9a-f]\{4\}/H' -e '${x;s/\n\S*//g;s/\s\.\.*//g;s/\s\([0-9a-f]\)\([0-9a-f]\)/\2\1/g;p}'
Or if your version of sed doesn't support separating commands with ;:
sed -n -e '/^0x[0-9a-f]\{4\}/H' -e '${x
s/\n\S*//g
s/\s\.\.*//g
s/\s\([0-9a-f]\)\([0-9a-f]\)/\2\1/g
p
}'
This works by adding each byte line to the hold space, and then when we get to the last line of the file swap the hold and pattern spaces to process them all at once. Then the steps are to remove the newlines and address from beginning of each line, remove the trailing dots (maybe this isn't actually necessary, depending on actual output), and finally swap the digits of each byte and print.
Depending on what version of Linux you are running, there are tools like od or hexdump that can help with this. hexdump is even highly configurable with a sort of mini-script language to control how many bytes get formatted in what way and so on...
One way using GNU awk:
awk '/^0x/ { for (i=2; i<=NF; i++) { gsub(/[^0-9]/,"", $i); line=line $i } } END { printf "%s\n", substr(line,2) }' file.txt

Convert hex stream to GIF

How do I create a .gif file from the following HEX stream:
0d 0a 0d 0a 47 49 46 38 39 61 01 00 01 00 80 ff 00 ff ff ff 00 00 00 2c 00 00 00 00 01 00 01 00 00 02 02 44 01 00 3b
3b is the GIF file terminator
I'm trying to do it following the guide at http://en.wikipedia.org/wiki/Graphics_Interchange_Format#Example_GIF_file
I'd like to implement this in either Perl or C/C++. Any language will do though.
Many thanks in advance,
Thanks guys for all the replies. I removed the leading '0d 0a 0d 0a'...
Here's what I have sofar:
#!/usr/bin/perl
use strict;
use warnings;
open(IN,"<test.txt");
open(OUT,">test.gif");
my #lines=<IN>;
foreach my $line (#lines){
$line=~s/\n//g;
my #bytes=split(/ /,$line);
foreach my $byte (#bytes){
print OUT $byte;
}
}
close(OUT);
close(IN);
You can do it in the shell, using GNU echo:
$ /bin/echo -n -e "\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\xff\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b" > foo.gif
$ identify foo.gif
foo.gif GIF 1x1 1x1+0+0 8-bit PseudoClass 2c 36B 0.000u 0:00.000
You can also use the xxd command which will "make a hexdump or do the reverse". Annoyingly, however, it seems very picky about the input format. Here's an example using xxd:
$ cat > mygif.hex <<END
0000000: 4749 4638 3961 0100 0100 80ff 00ff ffff
0000010: 0000 002c 0000 0000 0100 0100 0002 0244
0000020: 0100 3b0a
END
$ xxd -r < mygif.hex > mygif.gif
gvim has an interface to xxd. Use the "Tools → Convert To Hex" menu option (keyboard: :%!xxd) and then "Tools → Convert Back" (:%!xxd -r).
EMACS also has a built-in hex editor, which is accessed by M-x hexl-mode (see Editing Binary Files in the manual). It's also a little bit annoying, because you have to type C-M-x (i.e. Ctrl-Meta-X) before entering a character by its hex code:
Of course, it is very easy to write a simple C program to do the conversion:
#include <stdio.h>
int main(int argc, char **argv) {
unsigned int c;
while (1 == scanf("%x", &c))
putchar(c);
return 0;
}
usage:
$ gcc -Wall unhexify.c -o unhexify
$ echo "47 49 46 38 39 61 01 00 01 00 80 ff
00 ff ff ff 00 00 00 2c 00 00 00 00
01 00 01 00 00 02 02 44 01 00 3b" | ./unhexify > mygif.gif
Also: many answers here in this code golf question.
Open a new file for writing (in binary mode) with the .gif extension.
Read each pair of hex characters.
Convert the hex to a byte (char) value.
Write the byte to the opened file.
When finished, close the file.
If the hex data represents a GIF image, the file should contain it.
perl -ne'
BEGIN { binmode STDOUT }
s/\s//g;
print pack "H*", $_;
' file.hex > file.gif
Perl 5.14:
perl -ne'
BEGIN { binmode STDOUT }
print pack "H*", s/\s//gr;
' file.hex > file.gif
(-n and print can be replaced with -p and $_ = if you want to golf (shorten the length of the program.)
You may want to read the documentation for unpack (or hex) and pack. You may also find the perlio documentation useful for creating a raw file handle (so perl doesn't try to help you with things like encodings or line endings).