grep line from text file including brackets [duplicate] - linux

This question already has answers here:
grep to find literal [square brackets] with specific data
(2 answers)
Closed last month.
I have a file called a.txt
something [low] https://url/low.txt
something [info] http://url1/low.txt
something [low] https://url2/robots.txt
How can i achieve this output using grep command?
something [low] https://url/low.txt
something [low] https://url2/robots.txt
thanks in advance

grep 'something \[low\]' a.txt
The single quotes are to prevent the shell from interpreting the brackets (and breaking on the space).
grep itself uses brackets for simple regular expressions, so the brackets need to be escaped separately, inside the single quotes, to not have grep interpret them as special.
Using grep -F or fgrep doesn't use regular expressions: everything to grep on is taken as a single string pattern. The single quotes would still be necessary to prevent the shell from interpreting the brackets specially.

Related

Get text only within parenthesis from a file in linux terminal [duplicate]

This question already has an answer here:
How can I extract the content between two brackets?
(1 answer)
Closed 4 years ago.
I have a large log file I need to sort, I want to extract the text between parentheses. The format is something like this:
<#44541545451865156> (example#6144) has left the server!
How would I go about extracting "example#6144"?
This sed should work here:
sed -E -n 's/.*\((.*)\).*$/\1/p' file_name
There are many ways to skin this cat.
Assuming you always have only one lexeme in parentheses, you can use bash parameter expansion:
while read t; do echo $(t=${t#*(}; echo ${t%)*}); done <logfile
The first substitution: ${t#*(} cuts off everything up and including the left parenthesis, leaving you with example#6144) has left the server!; the second one: ${t%)*} cuts off the right parenthesis and everything after that.
Alternatively, you can also use awk:
awk -F'[)(]' '{print $2}' logfile
-F'[)(]' tells awk to use either parenthesis as the field delimiter, so it splits the input string into three tokens: <#44541545451865156>, example#6144, and has left the server!; then {print $2} instructs it to print the second token.
cut would also do:
cut -d'(' -f 2 logfile | cut -d')' -f 1
Try this:
sed -e 's/^.*(\([^()]*\)).*$/\1/' <logfile
The /^.*(\([^()]*\)).*$/ is a regular expression or regex. Regexes are hard to read until you get used to them, but are most useful for extracting text by pattern, as you are doing here.

Apend the lines in a config file using shell script [duplicate]

This question already has answers here:
How to escape single quote in sed?
(9 answers)
Closed 6 years ago.
I have a config.js file which contents below strings
.constant('Digin_Engine_API', 'http://local.net:1929/')
I want to read this file and replace what ever the things which are there after the .constant('Digin_Engine_API'I tried using sedbut ddnt worked. This is what I used for sed
sed -i 's/^.constant('Digin_Engine_API', .*/http://cloud.lk:8080/' /var/config.js
As a summary my final out put (config.js) file needs to consists below.
Before
.constant('Digin_Engine_API', 'http://local.net:1929/')
After
.constant('Digin_Engine_API', 'http://cloud.lk:8080/')
You need to use double quotes around sed command since single quote is part of pattern
You should use an alternate delimiter since / is used in replacement
You need to capture the first part and use it in replacement
You need to quote the replacement and also add closing )
Sed command:
sed -i.bak "s~\(\.constant('Digin_Engine_API', \).*~\1'http://cloud.lk:8080')~" /var/config.js
cat /var/config.js
.constant('Digin_Engine_API', 'http://cloud.lk:8080')
Here you are:
sed -i -r "s_(\.constant\('Digin_Engine_API').*_\1, <new content>)_" file
Remarks
you cannot use ' in sed command if command is surrounded by '' also,
you must escape all ( and ) that are part or string, and not the sed grouping command,
you must escape . character, because it is also sed replacement for every char,
you must use another sed s separator instead of / if you need to use / in that command, but you can also escape / by \/.

Write into a certain line number of a file using a variable [duplicate]

This question already has answers here:
How to insert a line using sed before a pattern and after a line number?
(5 answers)
Closed 6 years ago.
In a shell script, I get the certain number of a line in a file and store it in a variable using the code below:
res=$(grep -n '123' somefile.txt | sed 's/^\([0-9]\+\):.*$/\1/')
Now I want to write in 3 lines after this line, so I used these codes :
sed -i '$res\anything' somefile.txt
sed -i '$resianything' somefile.txt
but they don't work. It seems that sed doesn't accept variables.
How can I solve it? Is there any alternative way?
Some tips before pointing out the duplication:
Variables won't be expanded within single quotes.
Variable name boundaries are not detected automatically. $resianything will not expand to the value of the variable res followed by the string ianything, but rather to the value of the variable with the name resianything, which presumably is empty. You can detect this kind of error by adding set -o nounset (and ideally -o errexit) at the top of your script.
You'll definitely want to escape the string you're inserting back into a sed command.
You seem to be asking why the $res isn't expanding. That isn't a sed issue; it's the shell. Typically bash &c. don't expand variables within single quotes. Try double quoting that bit.
Also, one method for separating your variable from following alphanumeric text is to use curly braces. Try ${res}anything instead of $resanything, for example.

convert this linux statement into a statement which is supported by windows command prompt

This is my statement supported by unix environment
"cat document.xml | grep \'<w:t\' | sed \'s/<[^<]*>//g\' | grep -v \'^[[:space:]]*$\'"
But I want to execute that statement in windows command prompt .
How do I do that? and what are the commands which are similar to cat, grep,sed .
please tell me the exact code supported for windows similar to above command
The double quotes around the pipeline in your question are a syntax error, and the backslashed single quotes should apparently really not have backslashes, but I assume it's just an artefact of a slightly imprecise presentation.
Here's what the code does.
cat document.xml |
This is a useless use of cat but its purpose is to feed the contents of this file into the pipeline.
grep '<w:t' |
This looks for lines containing the literal string <w:t (probably the start of a tag in the XML format in the file). The single quotes quote the string so that it is not interpreted by the shell (otherwise the < would be interpreted as a redirection operator); they are consumed by the shell, and not passed through to grep.
sed 's/<[^<]*>//g' |
This replaces every pair of open/close brokets with an empty string. The regular expression [^<]* matches zero or more occurrences of a character which can be anything except <. If the XML is well-formed, these should always occur in pairs, and so we effectively remove all XML tags.
grep -v '^[[:space:]]*$'
This removes any line which is empty or consists entirely of whitespace.
Because sed is a superset of grep, the program could easily be rephrased as a single sed script. Perhaps the easiest solution for your immediate problem would be to obtain a copy of sed for your platform.
sed -e '/<w:t/!d' -e 's/<[^<]*>//g' -e '/[^[:space]]/!d' document.xml
I understand quoting rules on Windows may be different; try with double quotes instead of single, or put the script in a file and use sed -f file document.xml where file contains the script itself, like this:
/<w:t/!d
s/<[^<]*>//g
/[^[:space]]/!d
This is a rather crude way to extract the CDATA from an XML document, anyway; perhaps some XML processor would be the proper way forward. E.g. xmlstarlet appears to be available for Windows. It works even if the XML input doesn't have the beginning and ending <w:t> tags on the same line, with nothing else on it. (In fact, parsing XML with line-oriented tools is a massive antipattern.)
May try with "powershell" ?
It is included since Win8 I think,
for sure on W10 it is.
I've just tested a "cat" command and it works.
"grep" don't but may be adapt like this :
PowerShell equivalent to grep -f
and
https://communary.wordpress.com/2014/11/10/grep-the-powershell-way/
The equivalent of grep on windows would be findstr and the equivalent of cat would be type.

Grep : find patterns in text file

I have to find the following patterns in a text file
\xc2d
d\xa0
\xe7
\xc3\ufffdd
\xc3\ufffdd
\xc2\xa0
\xc3\xa7
\xa0\xa0
I start with finding \x occurrences and do this
grep "\\x" *.log | more
and nothing returns, is this query correct?
I think you'll want to use single quotes instead of double quotes.
grep '\\x' *.log | more
Your shell is likely stripping that first backslash as part of the processing it does for strings in double quotes, which grep needs.

Resources