Can I replace hours in sed command with variable? [Case Closed] [duplicate] - bash

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 6 years ago.
I'm trying to get a certain timestamps (in my case every 15 minute) in logfile using sed command in bash scripting.
My question is, can I replace the hours in the command with a variable?
This is script that I want:
#!/bin/bash
hour=`date +%H` #Current Hours e.g 14:00
sed -n '/$hour:15:00/,/$hour:30:00/p' file.log | nice grep Event
The result will print the logfile from 14:15:00 until 14:30:00. But there's a problem when the range is from 45 minute to 60 minute which is 14:45 - 15:00. Is there any solution for this?
UPDATE
This issue is already solved, the command below work for me.
sed -n "/${hour}:15:00/,/${hour}:30:00/p" file.log | nice grep Event
Other reference: Replace a string in shell script using a variable
Thank you.
== Case closed ==

Well, to answer the question - yes, you would take the variable out of the quotes and then it should use the value:
sed -n '/'$hour:15:00/,/'$hour':30:00/p' file.log | nice grep Event
You could also just use double quotes around the expression
sed -n "/${hour}:15:00/,/${hour}:30:00/p" file.log | nice grep Event

Related

How to remove last n character from string in shell script [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 21 days ago.
I have a variable reponame in Shell Script(bash) holding a string
echo $reponame
"testrepo.git"
I want to remove the last 4 character of this string and assign the result to a new variable repo
echo $repo
"testrepo"
How can I do this?
If I understand correctly, you want to get rid of the .git extension. Then the correct expression would be
repo=${reponame%.*}
or
repo=${reponame%.git}
for that very specific case.
For substrings in general, the expression removing last 4 characters would go like
repo=${reponame:0:-4}
Very nice resource on Bash string operations:
https://tldp.org/LDP/abs/html/string-manipulation.html
For shell in general you might use various approaches such as
repo=$(echo -n "$reponame" | sed 's/\.git$//')
or
repo=$(echo -n "$reponame" | rev | cut -f 2- -d '.' | rev)

How to perform arithmetic on every row of a file via piping [duplicate]

This question already has answers here:
Bash Script- adding numbers to each line in file
(4 answers)
Closed 12 months ago.
Lets say we have a simple file data.txt which has the following content:
1
2
3
Is there a command to perform arithmetic on every row value of the file via piping? I'm looking for something like cat data.txt | arit "+10" which would output:
11
12
13
The best thing I know is performing arithmetic by using bc for individual values, for example echo "1+10" | bc, but I wasn't able to apply this to my own example which contains many values with trailing newlines.
You could use awk:
awk '{print $1+10}' data.txt
My first impulse is to use sed. (Hey, it's the tool I always reach for.)
cat data.txt | sed 's/$/+10/' | bc
EDIT: or like so
sed 's/$/+10/' data.txt | bc

Bash. How to assign random line from text file to a variable? [duplicate]

This question already has answers here:
What's an easy way to read random line from a file?
(13 answers)
Closed 3 years ago.
I have text file which contains numerous lines. I need to do:
shuf txt.txt
From shuf output read first line to a variable $line
How to represent it in one line for bash script?
Now it is like this:
shuf txt.txt -o aaa.txt
n=$(head -n 1 aaa.txt)
rm -rf aaa.txt
As you may notice, it is not very nice
You can easily do this with shuf:
n=$(shuf -n1 file)
↳ https://www.gnu.org/software/coreutils/manual/html_node/shuf-invocation.html
Related question on stack overflow.com: What's an easy way to read random line from a file in Unix command line?
Sounds like homework. Here's some hints.
Assigning a variable to the result of a command:
x=`command`
Assignment with pipe:
x=`command1 | command2`
Assigning the first line of command1's output to a variable:
x=`command1 | head -n1`

Using grep to filter real time output of a process? If so, how to get the line after a match? [duplicate]

This question already has answers here:
How to show only next line after the matched one?
(14 answers)
grep: show lines surrounding each match
(14 answers)
Read from a endless pipe bash [duplicate]
(1 answer)
Closed 4 years ago.
Should I use grep to filter a real time output? I'm not sure if this is what I should use for a real time output.
Example: command -option | grep --color 'string1\|string2'
If so, how to get also the lines after string1 and string2?
As #shellter mentioned, from man grep:
-A num, --after-context=num
Print num lines of trailing context after each match. See also the -B and -C options.
so you would use command -option | grep -A 1 --color 'string1\|string2' to print matched lines and the line right after them.
There are plenty of other options in the manual for grep, and most other command-line programs, so I suggest getting used to running man cmd as a quick first check.

Does sed have an option just for checking existence of string in file? [duplicate]

This question already has answers here:
Return value of sed for no match
(9 answers)
Closed 9 years ago.
Does sed have an option similar to grep -q, where it returns a 1 or 0 depending on if it actually finds a string match or not?
The reason I ask is in some cases I use sed in particular ways, like finding the first match after a given line number, or setting a capture group and acquiring a string that way, so I wanted to see if sed had anything built in that would check if anything is returned because I want to throw an error back to the user and exit my bash script if a match isn't found
You are much better off using grep -q for this, but if you ABSOLUTELY want to use sed then here is how you could replicate the functionality...
cat myFile | sed -n "/myString/p" | wc -l
This will pipe out the number of occurences of "myString" in a file. From here you could do a simple test to see if the number is greater then 0, if so return 1, if not return 0. Here is a script demoing how to accomplish this...
#!/bin/bash
count=$(cat myFile | sed -n "/\myString/p" | wc -l)
final=0
if [ count -gt 0 ]; then
final=1
else
final=count
fi
echo final
The script above will print out 1 or 0, 1 if the string was found and 0 if it wasn't (note: I haven't checked this, but there's no reason why it wouldn't work).
Again, grep -q is the tool for the job and I would recommend using it if at all possible.
You can set the exit code using the q command in sed.
q [exit-code]
Immediately quit the sed script without processing any more input, except that if auto-print is not disabled the current pattern space
will be printed. The exit code argument is a GNU extension.
See also this answer for a clue.
EDIT:
As #cbuckley kindly linked to, you can find directions here.

Resources