grep the log file after running script and need to show the log file - linux

I need to run the script grep the logfile and then show the log file. I have tried the following command
sh abc.sh | grep "Log File: "| sed -e s/'Log File (.*)$')
I got an error as: sed expression #1 unterminated s' command

The syntax of your sed command is invalid. It's not entirely clear to me what you are trying to do with that invalid sed, but I think you're looking for:
sh abc.sh | sed -n -e '/Log File:/s///p'
This will suppress all output except those lines which match the regex Log File:, and the text Log File: will be replaced with the empty string. (s///p will use the previous pattern for the match, and replace the first occurrence of that pattern with the empty string. The p flag causes the line to be printed.)

Related

How to insert a new line between 2 specific characters

Is there anyway to insert new lines in-between 2 specific sets of characters?
I want to insert a new line every time }{ occurs in a text file, however I want this new line to be inserted between the 2 curly brackets.
For example }\n{
You can run
sed -i -e 's/}{/}\n{/g' filename.ext
where
sed is your stream editor program
-i is the option to edit file filename.ext in place
-e means that a regular expression follows
s/}{/}\n{/g is the regular expression meaning find all (g) instances of }{ in every line and replace them with }\n{ where \n is the regex for new line. If you omit g, it will only replace the first occurrence of the search pattern but still in every line.
To test before committing changes to your file, omit the -i option and it will print the result in STDOUT.
Example:
Create file:
echo "First }{ Last" > repltest.txt
Run
sed -e 's/}{/}\n{/g' repltest.txt
Prints the following to STDOUT:
First }
{ Last
To effect the change in the same file, use -i option.
To run this against STDIN instead of a file, omit -i and the filename in the piped command after something that outputs STDIN, for example:
cat repltest.txt | sed -e 's/}{/}\n{/g'
which does the same as sed -e 's/}{/}\n{/g' repltest.txt
Use sed.
sed [-i] -e 's/}{/}\n{/g' file.txt
Every instance of }{ will be replaced with }\n{. You can use the -i flag if you want to replace the text in the file.

sed removes one string in one file

I'm trying to remove one string in one file by using:
sed -i -e '/example/' test.txt
But I've got the following error:
sed: -e expression #1, char 6: missing command
What is it missing and why?
Thanks!
/example/ is an address which tells sed where to run commands - on a line containing the string example. You didn't specify any commands.
This is a commnand that replaces the string example with an empty string:
's/example//'
try:
sed '/example/d' test.txt
Explanation as suggested by #Nathan Tuggy
sed will search for the given string and will 'D'elete it
user#host:~$ cat test.txt
one
two
three
user#host:~$ sed '/two/d' test.txt
one
three

shell command delete line in text file with specific text in line

In looking for a command to delete a line (or lines) from a text file that contain a certain string.
For example
I have a text file as follows
Sat 21-12-2014,10.21,78%
Sat 21-12-2014,11.21,60%
Sun 22-12-2014,09.09,21%
I want to delete all lines that have "21-12-2014" in them.
I'm not able to find a solution that works.
According to #twalberg there is more three alternate solution for this question, which I'm explaining is as follows for future reader of this question for more versatile solutions:
With grep command
grep -v 21-12-2014 filename.txt
explanations:
-v is used to find non-matching lines
With awk command
awk '! /21-12-2014/' filename.txt
explanations:
! is denoting it will print all other lines that contain match of the string. It is not operator signify ignorance.
With sed command
sed -e '/21-12-2014/d' < filename.txt
explanations:
-e is signify scripted regex to be executed
d is denoting delete any match
< is redirecting the input file content to command
Try doing this :
sed -i.bak '/21-12-2014/d' *
A bit of explanations :
sed : the main command line, put the mouse pointer on sed
-i.bak : replace the file in place and make a backup in a .bak file
// is the regex
d means: delete

Sed Command get text between two string while excluding the two strings using Terminal UNIX

So I have a sed command that looks like this:
sed -n "/DXImageTransform.Microsoft.AlphaImageLoader(src='/,/',sizingMethod='crop');/p"
/Users/ME/Documents/weather/yahooWeather.html > /Users/ME/Documents/weather/out.txt
And it correctly gets the HTML that I want out of the file, however it still has the strings that I used to search for it (i.e. "DXImageTransform.Microsoft.AlphaImageLoader(src='" & "',sizingMethod='crop');" on the beginning and the end of the file. I'd like to remove those two strings. How can I modify my command to do this?
This should work:
sed -n -e "1,/START/d" -e "/END/,$d" -e p file.html
I separated 3 commands there using the -e options but you could put them all together separated by ;.
And this is what they do:
1,/START/d -- deletes a range: from the first line of the file until the line matching the pattern /START/
/END/,$d -- deletes a range: from the line matching the pattern until the end of the file
p -- print the line (these are lines not matched by previous patterns)
UPDATE
If the pattern is on the first line, the above won't work. With GNU sed, you can fix that like this:
sed -n -e "0,/START/d" -e "/END/,$d" -e p file.html
Unfortunately this won't work with BSD sed.

Trying to pass regular expression to grep

I'm trying to exctract error lines from my log file:
I used this :
more report.txt | grep -E (?i)(error)
I'm getting this error msg :
bash: syntax error near unexpected token `('
What am I doing wrong? I'm trying to extract all lines containing "Error" ignoring the case sensitivity so it can be error, Error, ERROR etc.
The problem with your line is that the parens are picked up by the shell instead of grep, you need to quote them:
grep -E '(?i)(error)' report.txt
For this particular task the other answers are of course correct, you don't even need the parens.
You can do:
grep -i error report.txt
There is really no need to more the file and then pipe it to grep. You can pass the filename as an argument to grep.
To make the search case insensitive, you use the -i option of grep.
And there is really no need to go for -E option as the pattern you are using error is not an extended regex.
The cause of the error you are seeing is that your pattern (?i)(error) is interpreted by the shell and since the shell did not except to see ( in this context it throws an error, something similar to when you do ls (*).
To fix this you quote your pattern. But that will not solve your problem of finding error in the file because the pattern (?i)(error) looks for the string 'ierror' !!
you can use
grep -i error report.txt
this will achieve the same result
cat report.txt | grep -i error
and if you want to paginate the results:
cat report.txt | grep -i error | more

Resources