Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove spaces between patterns

Tags:

bash

sed

I have a log file where data is separated by spaces. Unfortunately one of the datafields contains spaces as well. I would like to replace those spaces with "%20". It looks like this:

2012-11-02 23:48:36 INFO 10.2.3.23 something strange name.doc 3.0.0 view1 orientation_right

the expected result is

2012-11-02 23:48:36 INFO 10.2.3.23 something%20strange%20name.doc 3.0.0 view1 orientation_right

unpredictable that how many spaces we have between the IP address and ".doc". So I would like to change them between these two patterns using pure bash if possible.

thanks for the help

like image 687
user1797649 Avatar asked Feb 19 '26 14:02

user1797649


2 Answers

$ cat file
2012-11-02 23:48:36 INFO 10.2.3.23 something strange name.doc 3.0.0 view1 orientation_right

Using Perl:

$ perl -lne 'if (/(.*([0-9]{1,3}\.){3}[0-9]{1,3} )(.*)(.doc.*)/){($a,$b,$c)=($1,$3,$4);$b=~s/ /%20/g;print $a.$b.$c;}' file
2012-11-02 23:48:36 INFO 10.2.3.23 something%20strange%20name.doc 3.0.0 view1 orientation_right
like image 170
Guru Avatar answered Feb 22 '26 04:02

Guru


This might work for you (GNU sed):

sed 's/\S*\s/&\n/4;s/\(\s\S*\)\{3\}$/\n&/;h;s/ /%20/g;H;g;s/\(\n.*\n\)\(.*\)\n.*\n\(.*\)\n.*/\3\2/' file

This splits the line into three, copies the line, replaces space's with %20's in one of the copies and reassembles the line discarding the unwanted pieces.

EDIT:

With reference to the comment below, the above solution can be ameliorated to:

sed -r 's/\S*\s/&\n/4;s/.*\.doc/&\n/;h;s/ /%20/g;H;g;s/(\n.*\n)(.*)\n.*\n(.*)\n.*/\3\2/' file
like image 21
potong Avatar answered Feb 22 '26 03:02

potong