replace 8 times successive spaces by |
1 Attachment(s)
Hi
I am trying to make GUI for cmdow Attached here is the log. My aim is to separate columns by | which is understood by 'autoit' software. i have attached the log. The problem is I have to replace one or more consecutive spaces by a single | . And the title of window may contain spaces but that has to be discarded. Is there a way using command line that I replace ' \+' by '|' only 8 times in each line ? Thank You. |
Hi,
a somewhat convoluted sed will do: Code:
sed -r 'h;s/[[:blank:]]+/|/g;x;s/([^[:blank:]]+[[:blank:]]+){8}//;x;s/\|[^|]+//g8;G;s/\n/|/' cmdow-log Code:
sed -r 's/[[:blank:]]+/|/g;s/\|/ /g9' cmdow-log |
if you would wrote:
I have a file: Code:
0x010064 1 2880 Res Ina Ena Hid explorer WorkerW it would be understood, but saying "The problem is I have to replace one or more consecutive spaces by a single | " I understand like you have " ...8x... " spaces and need to replace it with single "|". Of course it can be done with many commands like "sed" or "awk" @crts NICE WORK ! |
Not sure what you wanted to do about the Header line, but the following will have the rest the way you want:
Code:
ruby -ane '(0..($F.length - 1)).each{|i| $F[i]+=(i<=7)?"|":" "};puts $F.join' cmdow-log |
to crts
Code:
sed -r 'h;s/[[:blank:]]+/|/g;x;s/([^[:blank:]]+[[:blank:]]+){8}//;x;s/\|[^|]+//g8;G;s/\n/|/' cmdow-log Actually i am using unxutils on windows which has no binary for 'ruby'. I will try to decipher the code. |
Can you explain me this ?
Code:
echo "123 abc" | sed 's/[0-9]*/& &/' |
The issue is where the match starts as you are using asterisk, meaning zero or more, it is looking
from left to right and saying that there are zero digits at the front of the string so replace that with itself, a space and itself. So 2 lots of nothing with a space leaves you with a space at the start. What makes less sense to me however is: Code:
$ echo "abc 123" | sed 's/[0-9][0-9]*/& &/' |
I get it .
I was reading this sed totorial by Bruce Barnett. I haven't used g flag so first occurence is substituted & rest printed as it is. so 'abc 123' is actually ^abc 123$ thus output is '^ ^abc 123' where ^ can be called nothing. |
to grail
I think Code:
echo abc 123 | sed 's/[0-9]\+/& &/' gives |
also I found a solution to question I asked .
It is simple,workable though it removes extra spaces from last column Code:
sed -e "s/ \+/|/g" -e "s/|/ /9g" cmdow-log.txt |
Or you could just use '-r' switch. What I presented does confuse me a little but I do also know that it is an extended regular expression solution (I was trying to direct you to this):
Code:
$ echo "abc 123" | sed -r 's/[0-9]+/& &/' |
about '-r' option
So, reading the last few posts there appear to be some misconceptions about the '-r' option and extended RegEx in GNU sed.
The most important thing to notice is that GNU sed by default understands extended RegEx. Supplying the '-r' option does not add any additional functionality. It simply avoids the need for escaping them. E.g., the "+" is an extended RegEx. To have sed interpret it as such you have to prepend a backslash, like "\+". Using the -r option simply makes the backslash obsolete in most cases: Code:
echo 'word' | sed 's/w\+/C/' Code:
echo 'word word' | sed 's/\(word\) \1/\1 CHANGE/' Code:
echo 'word' | sed 's/\<w.*\>/CHANGE/' @OP: You said that you are reading the tutorial by Bruce Barnett. I suppose you mean the tutorials on this site: http://www.grymoire.com/Unix You might get a bit confused when you read the tutorial about Regex in general on that site, especially this chapter: http://www.grymoire.com/Unix/Regular.html#uh-12 It says that "\{" and "\}" are basic RegEx and that they cannot be used as extended RegEx. However, in the table further down it is marked as extended RegEx. This is contradictory. Anyway, RegExes are a great source for confusion since every language/program seems to add its own small modifications to them. BTW, this is how sed handles "{}": Code:
echo 'hello' | sed 's/l\{2\}/CC/' |
Thanks for the clarity crts :)
|
All times are GMT -5. The time now is 07:34 PM. |