grep match end of line - linux

I have bunch of files containing the lines having label="some value". what I would like to do is to fetch those lines which does not have ", /> or as a matter of fact anything after that.
desired o/p
label="Relation From a Connector
I am doing something like this:
grep -rni "label="\" * | grep -v \"$
and getting this non desired o/p
label="Graceful Restart Helper"/>
label="BGP Route Reflector Policy"

This could be due to "\r\n" line endings in which case grep -v \"$ will not trigger because there is a carriage return character after the " and before the end of line. If this is the case then filter through
grep -E -v '"^M?$'
where ^M is control-M.

Related

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

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.)

Fetching the value of variable stored in a file

I am trying to fetch the output of a variable stored in a file in another shell script.
Example:
cat abc.log
var1=2
var2=2
var3=25
I am writing a script to fetch the value of var3.
Thank you in advance.
awk -F= '$1 ~ /^[[:space:]]*var3/ { print $2 }' abc.log
Set the field delimiter to = and then where the line contains "var3", print the second field.
Alternatively, you could:
source abc.log
and then:
echo $var3
Using sed you can isolate 25 with particularity with:
sed -n '/^[[:space:]]*var3=/s/^[^=]*=//p' file
Explanation
This is the general substitution form s/find/replace/ with a matching expression preceding it. The total form is /match/s/find/replace/. The option -n suppresses the normal printing of pattern-space and the p at the end tells sed to print the line where the match and substitution took place. Specifically,
/match/ locates a line with any number of preceding whitespace characters followed by var3=. The POSIX [:space:] character class matches any whitespace,
the /find/ is all characters anchored from the '^' beginning that are not the [^=] character and then match the literal '=' character, and finally
the /replace/ is the empty-string leaving the 25 alone which is printed.
Example Use/Output
$ sed -n '/^[[:space:]]*var3=/s/^[^=]*=//p' file
25
A grep one-liner, if your grep has support for Perl-compatible regular expressions (the -P option; not all greps support that)
grep -Po '^\s*var3=\K.*' abc.log
or,
grep -Po '^\s*var3=\K.*' abc.log | tail -n1
in order to get the last value of the var3, if multiple var3s is a possibility.

How to remove line breaks generated by sed

I have a file called sms:
gsm versi jadul
29 sender: +62896666666
date: 15/02/14,03:55:12
reboot router
when I type in:
sed -n '6p' sms > /tmp/result
The /tmp/result always looks like this:
Notice the line break there, I want to get rid of the line break on the second line, so the final result will be like this:
How do I do that?
You could trim it off with tr like this:
sed -n '6p' sms | tr -d '\n' > /tmp/result
You can use awk instead of sed:
awk 'NR==6 {printf $0}' sms > result
NR==6 specifies line number
printf $0 prints that line without any \n
There's nothing wrong with your sed command, your input file contains trailing control-Ms. Remove them with dos2unix or similar before running sed.
A correct implementation of the POSIX sed command does not add such a blank line. 6p should print the sixth line. I cannot reproduce the issue on, for example, Ubuntu 12 Linux. You have some line ending problem or some such issue.

Output grep results to text file, need cleaner output

When using the Grep command to find a search string in a set of files, how do I dump the results to a text file?
Also is there a switch for the Grep command that provides cleaner results for better readability, such as a line feed between each entry or a way to justify file names and search results?
For instance, a away to change...
./file/path: first result
./another/file/path: second result
./a/third/file/path/here: third result
to
./file/path: first result
./another/file/path: second result
./a/third/file/path/here: third result
grep -n "YOUR SEARCH STRING" * > output-file
The -n will print the line number and the > will redirect grep-results to the output-file.
If you want to "clean" the results you can filter them using pipe | for example:
grep -n "test" * | grep -v "mytest" > output-file
will match all the lines that have the string "test" except the lines that match the string "mytest" (that's the switch -v) - and will redirect the result to an output file.
A few good grep-tips can be found in this post
Redirection of program output is performed by the shell.
grep ... > output.txt
grep has no mechanism for adding blank lines between each match, but does provide options such as context around the matched line and colorization of the match itself. See the grep(1) man page for details, specifically the -C and --color options.
To add a blank line between lines of text in grep output to make it easier to read, pipe (|) it through sed:
grep text-to-search-for file-to-grep | sed G

How to grep curl -I header information

I'm trying to get the redirect link from a site by using curl -I then grep to "location" and then sed out the location text so that I am left with the URL.
But this doesn't work. It outputs the URL to screen and doesn't put it
test=$(curl -I "http://www.redirectURL.com/" 2> /dev/null | grep "location" | sed -E 's/location:[ ]+//g')
echo "1..$test..2"
Which then outputs:
..2http://www.newURLfromRedirect.com/bla
What's going on?
As #user353852 points out, you have a carriage return character in you output from curl that is only apparent when you try to echo any character after it. The less pager shows this up as ^M
You can use sed to remove "control characters", like in this example:
% test=$(curl -I "http://www.redirectURL.com/" 2>|/dev/null | awk '/^Location:/ { print $2 }' | sed -e 's/[[:cntrl:]]//') && echo "1..${test}..2"
1..http://www.redirecturl.com..2
Notes:
I used awk rather than your grep [...] | sed approach, saving one process.
For me, curl returns the location in a line starting with 'Location:' (with a capital 'L'), if your version is really reporting it with a lowercase 'l', then you may need to change the regular expression accordingly.
the "Location" http header starts with a capital L, try replacing that in your command.
UPDATE
OK, I have run both lines separately and each runs fine, except that it looks like the output from the curl command includes some control chars which is being captured in the variable. When this is later printed in the echo command, the $test variable is printed followed by carriage return to set the cursor to the start of the line and then ..2 is printed over the top of 1..
Check out the $test variable in less:
echo 1..$test..2 | less
less shows:
1..http://www.redirectURL.com/^M..2
where ^M is the carriage return character.

Resources