How to extract the date time from a string using bash - linux

I have the below strings and from those i need to extract the data and time. I were trying to get these using awk command i am unable to find our the solution. The below lined are the put put of
grep -ir 'can not save data' Aplication-aplicationName-server0046704* | awk -F' GMT' '{print$1}' | grep -v 'can not save data'
Aplication-aplicationName-server0046704.log.1:2020 May 27 10:23:16:147
Aplication-aplicationName-server0046704.log.bkp:2020 May 27 10:23:16:147
desired output :
2020 May 27 10:23:16:147

Using sed:
sed -E 's/^[^:]+://' | uniq
Explanation:
The sed command will remove everything before the first :
and the first :
The uniq command will remove every duplicate
Using cut:
cut -d ':' -f2- | uniq
Explanation:
cut will accept as delimeter a : (-d ':') and will print the fields
from the second to the end (-f2-)
The uniq command will remove every duplicate

Related

Extract all unique URL from log using sed

Can you help me with correct regexp from the sed syntaxis point of view? For now every regexp that i can write is marked by terminal as invalid.
If your log syntax is uniform, use this command
cut -f4 -d\" < logfile | sort -u
If you want to skip the query string from uniqness, use this
cut -f4 -d\" < logfile | cut -f1 -d\? | sort -u
Explanation
Filter the output with the cut command, take the 4th field (-f4) using " as separator (-d\"). The same with the second filter, using ? as separator

Use of cut to display information from last command linux

I am trying to fetch the user name and IP from where they logged in on my system.
I used the following command:
last -i | grep 'Jan 12' | cut -f1,3
But I am getting full line as the result.
But when I use awk :
last -i | grep 'Jan 12' | awk '{print $1, $3}'
I am getting the correct result.
Why wrong output in case of cut command ?
Any help would be highly appreciated.
Default delimiter of cut is a tab, whereas default input field separator in awk is any whitespace i.e. space or tab.
To get the same behavior in cut, you need to add -d ' ' in cut to make it:
last -i | grep 'Jan 12' | tr -s ' ' | cut -d ' ' -f1,3
tr -s ' ' is required to squeeze multiple spaces into a single space.
However using awk lets you skip grep altogether and use:
last -i | awk '/Jan 12/{print $1, $3}'
In cut, default delimiter is [Tab]. Also with -d key you can specify only a single character as delimiter.
In last output there are 8 spaces in a row.
So, the best way is to use awk as in your example.
Bad, but working solution with cut:
last | grep 'Jun 23' | sed 's/\s\s*/ /g' | cut -d' ' -f1,3

using linux cat and grep command

I am having following syntax for one of my file.Could you please anyone explain me what is this command doing
path = /document/values.txt
where we have different username specified e.g username1 = john,username2=marry
cat ${path} | grep -e username1 | cut -d'=' -f2`
my question here is cat command is reading from the file value of username1 but why why we need to use cut command?
Cat is printing the file. The file has username1=something in one of the lines. The cut command splits this and prints out the second argument.
your command was not written well. the cat is useless.
you can do:
grep -e pattern "$path"|cut ...
you can of course do it with single process with awk if you like. anyway the line in your question smells not good.
awk example:
awk -F'=' '/pattern/{print $2}' inputFile
cut -d'=' -f2`
This cut uses -d'=' that means you use '=' as 'field delimiter' and -f2 will take only de second field.
So in this case you want only the value after the "=" .

Bash grep output filename and line no without matches

I need to get a list of matches with grep including filename and line number but without the match string
I know that grep -Hl will give only file names and grep -Hno will give filename with only matching string. But those not ideal for me. I need to get a list without match but with line no. For this grep -Hln doesn't work. I tried with grep -Hn 'pattern' | cut -d " " -f 1 But it doesn't cut the filename and line no properly.
awk can do that in single command:
awk '/pattern/ {print FILENAME ":" NR}' *.txt
You were pointing it well with cut, only that you need the : field separator. Also, I think you need the first and second group. Hence, use:
grep -Hn 'pattern' files* | cut -d: -f1,2
Sample
$ grep -Hn a a*
a:3:are
a:10:bar
a:11:that
a23:1:hiya
$ grep -Hn a a* | cut -d: -f1,2
a:3
a:10
a:11
a23:1
I guess you want this, just line numbers:
grep -nh PATTERN /path/to/file | cut -d: -f1
example output:
12
23
234
...
Unfortunately you'll need to use cut here. There is no way to do it with pure grep.
Try
grep -RHn Studio 'pattern' | awk -F: '{print $1 , ":", $2}'

Script to replace tokens with values mentioned in properties file

I have a file values.properties which contain data, like:
$ABC=10
$XYZ=20
I want to create a shell script that will take each element one by one from above file.
Say $ABC, then go to file ABC.txt & replace the value of $ABC with 10.
Similarly, then go to file XYZ.txt and replace $XYZ with 20.
I think maybe this should be in the Unix and Linux section, the solution I've hacked together is as follows:
cat values.properties | grep "=" | cut -d "$" -f2 | awk -F "=" '{print "s/$"$1"/"$2"/g "$1".txt"}' | xargs -n2 sed -i
The flow is like so:
Filter out all the value assignments via: grep "="
Remove the '$' via: cut -d "$" -f2
Use awk to split the variable name and value and construct sed replacement command
Use xargs to pull in the replacement parameter and target file via: xargs -n2
Finally pass sed to as the command to xargs: xargs -n2 sed

Resources