Issue
I'm a beginner to bash scripting and been writing a script to check different log files and I'm bit stuck here.
clientlist=/path/to/logfile/which/consists/of/client/names
#i will grep only the client name from the file which has multiple log lines
clients=$(grep --color -i 'list of client assets:' $clientlist | cut -d":" -f1 )
echo "Clients : $clients"
#For example "Clients: Apple
# Samsung
# Nokia"
#number of clients may vary from time to time
assets=("$clients".log)
echo assets: "$assets"
The code above greps the client name from the log file and i'm trying to use the grepped client name (each) to construct a logfile with each client name.
The number of clients is indefinite and may vary from time to time.
The code I have returns the client name as a whole
assets: Apple
Samsung
Nokia.log
and I'm bit unsure of how to cut the string and pass it on one by one to return the assets which has .log
for each client name. How can i do this ?
Apple.log
Samsung.log
Nokia.log
Solution
(Apologies if I have misunderstood the task)
Using awk
if your input file (I'll call it clients.txt
) is:
Clients: Apple
Samsung
Nokia
The following awk
step:
awk '{print $NF".log"}' clients.txt
outputs:
Apple.log
Samsung.log
Nokia.log
(You can pipe straight into awk and omit the file name if the pipe stream is as the file contents in the above example).
It is highly likely that a simple awk
procedure can perform the entire task beginning with the 'clientlist' you process with grep
(awk
has all the functionality of grep
built-in) but I'd need to know the structure of the origial file to extract the client names.
Answered By - Dave Pritlove Answer Checked By - Katrina (WPSolving Volunteer)