I am using Ubuntu Linux and grepping info out of a file (lets say filename.log) and want to save the file using some of the info inside of (filename.log).
example:
The info in the (filename.log) has version_name and date.
When displaying this info on screen using cat it will display:
version_name=NAME
date=TODAY
I then want to save the file as NAME-TODAY.log and have no idea how to do this.
Any help will be appreciated
You can chain a bunch of basic linux commands with the pipe character |
. Combined with a thing called command substitution (taking the output of a complex command, to use in another command. syntax: $(your command)
) you can achieve what you want to do.
This is what I came up with, based on your question:
cp filename.log $(grep -E "(version_name=)|(date=)" filename.log | cut -f 2 -d = | tr '\n' '-' | rev | cut -c 2- | rev).log
So here I used cp
, $()
, grep
, cut
, tr
and finally rev
.
Since you said you had no idea where to start, let me walk you trough this oneliner:
cp
- it is used to copy the filename.log file to a new file, with the name based on the values of version_name
and date
(step 2 and up) $()
the entire command between the round brackets is 'resolved' before finishing the cp command in step 1. eg in your example it would be NAME-TODAY. notice the .log
at the end outside of the round brackets to give it a proper file extension. The output of this command in your example will be NAME-TODAY.log
grep -E "(version_name=)|(date=)"
grep with regexp flag -E
to be able to do what we are doing. Matches any lines that contain version_name=
OR date=
. The expected output is:version_name=NAME
date=TODAY
cut -f 2 -d =
because I am not interested in version_name
, but instead in the value associated with that field, I use cut to split the line at the equals character =
with the flag -d =
. I then select the value behind the equals character (the second field) with the flag -f 2
. The expected output is:NAME
TODAY
tr '\\n' '-'
because grep outputs on multiple lines, I want to remove all new lines and replace them with a dash. Expected output:NAME-TODAY-
rev | cut -c 2- | rev
rev | cut -c 2- | rev
I am grouping these. rev
reverses the word I have created. with cut -c 2-
I cut away all characters starting from the second character of the reversed word. This is required because I replaced new lines with dashes and this means I now have NAME-TODAY-
. Basicly this is just an extra step to remove the last dash. See expected outputs of each step:-YADOT-EMAN
YADOT-EMAN
NAME-TODAY
remember this value is in the command substituion of step 2, so the end result will be:
cp filename.log NAME-TODAY.log
我设法通过执行以下操作来解决此问题: grep filename.log > /tmp/file.info && filename=$(echo $(grep "version_name" /tmp/filename.info | cut -d " " -f 3)- $(grep "date" /tmp/filename.info | cut -d " " -f 3)-$filename.log
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.