How to Format grep Output When Saving to a Variable [duplicate] - linux

This question already has answers here:
How to preserve line breaks when storing command output to a variable?
(2 answers)
Closed 7 years ago.
If I grep our syslogs for a specific term, I get a nice output of those logs matching my term and each entry on a separate line.
If I save that to a variable so I can use it in a script as such:
results=$( grep "term" logs )
echo $results
then all the logs run together and are not human readable.
How can I make it look cleaner so when I do echo $results, I can actually read the output?
Thanks,

Quote it:
echo "$results"
This preserves all the whitespace, instead of using it for word splitting.
In general, you should almost always quote variables, unless you have a specific reason not to.

Related

Changing contents of a specific parameter in a file through shell script [duplicate]

This question already has answers here:
sed + replace only if match the first word in line
(3 answers)
Closed 1 year ago.
I have a file called local.conf:
db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://localhost:3306/xyz31"
db.default.user="ron"
db.default.password=""
Here xyz31 is the variable ${DB_NAME}. I need to update only the ${DB_NAME} which is xyz31 (in this instance), but which varies depending on what the current of value ${DB_NAME} is, and which needs to be updated to whatever value the user has entered, for ex:abc22.
db.default.driver="com.mysql.jdbc.Driver"
db.default.url="jdbc:mysql://localhost:3306/abc22"
db.default.user="ron"
db.default.password=""
Is there a way to achieve this?
Use an editor like Unix' sed(1):
sed -i -e 's;xyz31;abc22; /your/funny/file/here
Might need to decorate with word beginning/end to avoid false positive matches (check your manual what is available). And/or write out another file and check with diff(1) that the change is right.
sed should be enough:
i=abc22 # or input from user by read i
sed "/^db.default.url/s/$DB_NAME/$i/" local.conf
Add -i option if you want to make change to the file.

Matching a pattern in cat/zcat? [duplicate]

This question already has answers here:
bash wildcard n digits
(4 answers)
Closed 2 years ago.
How can I match a more complex pattern when using cat or zcat? For example, I know I can do this:
zcat /var/log/nginx/access.log.*.gz
Which will output all the gzipped access logs to stdin.
What if I want a more complex pattern? Say, all the log files that are between 1-15, e.g. something like this pattern:
zcat /var/log/nginx/access.log.([1-9]|1[0-5]).gz
This results in an unexpected token which is obvious, but I'm not sure how I'd escape the regex in this situation? Maybe I need to pipe ls output to zcat instead?
It depends of course on specifically what pattern you want to match. For the example you have given of log files 1-15, you could use something like
cat /var/log/nginx/access.log.{1..15}.gz
which will complain to stderr if any of those numbers don't exist, but it will still concatenate the rest to stdout.
This technique is a "sequence expression" if you want to look it up - it's a part of brace expansion.

Usage of '-' after pipe [duplicate]

This question already has answers here:
What's the magic of "-" (a dash) in command-line parameters?
(5 answers)
Closed 5 years ago.
I saw this line of code the other day and I didn't know exactly what is this - or when to use it. This is a simple code and from what I understood is that - takes the piped output and treats it as an argument of paste (correct me if I'am wrong)
seq $size | paste - $file
My question is when can we use this and is there another way to do the same thing?
Thanks,
This is documented in the man page for paste which says:
With no FILE, or when FILE is -, read standard input.
That is, paste expects you to give it a filename, but you can instead give it a single - instead, which paste will interpret as it should read data from stdin , your pipe in this case, instead of opening a file and read data from that file.

saving the output of a pipe to a variable [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 7 years ago.
This seems very easy (and it probably is), but I'm having some problems with saving a result of a pipe to a variable.
Let's say this is the output of the pipe:
This
is
the
output
of
the
pipe
Which look exactly as I want. However, if I try to store the pipe into variable:
var=$(...pipe...)
The output of the echo $var will be:
This is the output of the pipe
Also tried with printf, but it doesn't work either.
Your assignment is fine, but you need to quote the variable in echo:
echo "$var"
It is probably best practice to put quotes on the assignment and write var="$(...)", but it's not actually necessary since word splitting does not occur on the RHS of an assignment.

grok when to use quotes in shell scripting [duplicate]

This question already has answers here:
How do I learn how to get quoting right in bash?
(3 answers)
Closed 8 years ago.
When BASH scripting, I am often confused when to put variables in quotes or just have them called without quotes.
"$i" vs. $i
echo hello vs echo "hello"
-eq vs ==
$((i%2))
Could someone point me to a resource that explains this well or give me some basic tips? Thanks.
Here's the basic tip: always double-quote your variable usages in Bash, unless you know for sure you need to not quote them. Quoting them is safer, as it prevents word splitting; you will know during development when you need word splitting and therefore should not quote a variable.

Resources