Remove patterns from string using bash
7,449
You can also call perl
from bash
. The -n
arg makes it loop for each line. -e
means the script is one line.
cat in.txt | perl -ne 's{.*(.*\().*? (.*) -.*(:.*?)\s\s+.*(\[.*)}{$1$2$3 : $4};'
Related videos on Youtube
Author by
Chef Pharaoh
Updated on September 18, 2022Comments
-
Chef Pharaoh almost 2 years
I'm trying to remove some patterns from a file input string (parsing file line by line). Here is a sample string:
1: (10/17 12:49:31.175) - CONSTANT ID1 - CONSTANT ID2: RAW DATA OUT > [0x00,0xa2,…,0x00]
And I want the output to be something like the following:
(12:49:31.175): RAW DATA OUT : [0x00,0xa2,…,0x00]
I have been trying to use
egrep
orsed
but no luck so far, just keep getting errors or some "unterminated substitute pattern" error. Here is and example of what I have tried:echo $line | sed -e 's/.*\s\([0-9]*:[0-9]*:[0-9]*.[0-9]*\)'
Any help would be appreciated. I'm usually more of a batch file guy instead of bash on my Mac OS/X.
EDIT:
I should mention, I'm reading a file as such:
while read line do
Then I want to perform any actions on
line
for every line in the file. -
Chef Pharaoh over 9 yearsThis looks good but I keep getting "No such file or directory" error. I'm reading a file like so:
while read line
and then want to parseline
for each line in the file. -
Chef Pharaoh over 9 yearsOr now I'm getting
s/([^ ] ...": bad flag in substitute command: ')'
-
Chef Pharaoh over 9 yearsYup, this is exactly what I was looking for. I had an extra
-
in the CONSTANT ID so I had to add another-.*
in there. Although, I was hoping not to use perl and more regular scripting commands. -
Chef Pharaoh over 9 yearsCan I use this output to print to a file now? Haha, never mind, just a matter of using redirect.
-
mikeserv over 9 yearsAnyone trying to replicate your approach is going to be in for an ugly surprise if the
<<DATA
they feedsed
has any shell special characters in it. If you do not quote the heredoc limiter the shell will perform any expansions it can on the contents.