Looking to optimize code

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looking to optimize code
# 1  
Old 12-26-2015
Hammer & Screwdriver Looking to optimize code

Hi guys,

I feel a bit comfortable now doing bash scripting but I am worried that the way I do it is not optimized and I can do much better as to how I code.


I have a whole line in a file from which I want to extract some values.

Right now what I am doing is :
STATE=`cat /opt/scripts/start.log | awk '{print $3}'`
TIME=`cat /opt/scripts/start.log | awk '{print $2}' | cut -d, -f1 `
DATE=`cat /opt/scripts/start.log | awk '{print $1}'`

echo " The app started at $STATE $TIME with state $STATE."

Is this a good way to extract 3 words (strings) from a line of text ? Or is there a better/more optimized way of doing this ?
# 2  
Old 12-26-2015
At least you can avoid the execution of cat; awk can directly read the file.
It is a bit strange that your file seems to be a log file - what happens if it has more than one line?
The following efficient shell builtin reads one line into three variables
read DATE TIME STATE  < /opt/scripts/start.log

Might need some little post processing...

Last edited by MadeInGermany; 12-26-2015 at 03:19 PM.. Reason: changed order
# 3  
Old 12-26-2015
And the post processing MadeInGermany mentioned would be something like:

followed by the echo you already had.
# 4  
Old 12-26-2015
And that post processing that Don used is an example of what you can do with Parameter Substitution. There are many other ways you can carve a piece of information out of a string, without the need of invoking an external program, which it will always be slower that using the built-in shell functionalities.
# 5  
Old 12-26-2015
Finally: you won't need the subshell execution any more utilizing the advice you already got, but for future reference, in the cases where you do need it: the backticks should not be used any more! They are a leftover from the original Bourne shell, which is outdated by some 25 years now. Use the modern POSIX way of process substitution:

variable=`command1 | command2`     # old, do not use!
variable=$(command1 | command2)    # the modern way to do the same

I hope this helps.

# 6  
Old 12-27-2015
And, as there may be more than those three strings in a line, you might want to provide for a "sag wagon" for the trailing rest:
read DATE TIME STATE REST < /opt/scripts/start.log

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help Optimize the Script Further

Hi All, I have written a new script to check for DB space and size of dump log file before it can be imported into a Oracle DB. I'm relatively new to shell scripting. Please help me optimize this script further. (0 Replies)
Discussion started by: narayanv
0 Replies

2. Shell Programming and Scripting

Optimize awk code

sample data.file: 0,mfrh_green_screen,1454687485,383934,/PROD/G/cicsmrch/sys/unikixmain.log,37M,mfrh_green_screen,28961345,0,382962--383934 0,mfrh_green_screen,1454687785,386190,/PROD/G/cicsmrch/sys/unikixmain.log,37M,mfrh_green_screen,29139568,0,383934--386190... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Optimize my mv script

Hello, I'm wondering if there is a quicker way of doing this. Here is my mv script. d=/conversion/program/out cd $d ls $d > /home/tempuser/$$tmp while read line ; do a=`echo $line|cut -c1-5|sed "s/_//g"` b=`echo $line|cut -c16-21` if ;then mkdir... (13 Replies)
Discussion started by: whegra
13 Replies

4. Shell Programming and Scripting

Optimize awk command

WARNING=${1} CRITICAL=${2} echo ${OUTPUT} | gawk -F'' ' { V = $2 R = $0 } END { for ( i = 1; i <= n; i++) { if((V > 0) && (V < V)) print R, ((V - V) / V) * 100 else if ((V > V) && (V > 0)) ... (6 Replies)
Discussion started by: SkySmart
6 Replies

5. Shell Programming and Scripting

Can someone please help me optimize my code (script searches subdirectories)?

Here is my code. What it does is it reads an input file (input.txt which contains roughly 2,000 search phrases) and searches a directory for files that contains the search phrase. The directory contains roughly 1900 files and 84 subdirectories. The output is a file (output.txt) that shows only the... (23 Replies)
Discussion started by: jl487
23 Replies

6. Shell Programming and Scripting

Optimize the nested IF

Hi, I have to assign a value for a varaiable based on a Input. I have written the below code: if then nf=65 elif then nf=46 elif then nf=164 elif then nf=545 elif then nf=56 elif then (3 Replies)
Discussion started by: machomaddy
3 Replies

7. Shell Programming and Scripting

pl help me to Optimize the given code

Pl help to me to write the below code in a simple way ... i suupose to use this code 3 to 4 places in my makefile(gnu) .. **************************************** @for i in $(LIST_A); do \ for j in $(LIST_B); do\ if ;then\ echo "Need to sign"\ echo "List A = $$i , List B =$$j"\ ... (2 Replies)
Discussion started by: pk_arun
2 Replies

8. Shell Programming and Scripting

Optimize shell code

#!/usr/bin/perl use strict; use warnings; use Date::Manip; my $date_converted = UnixDate(ParseDate("3 days ago"),"%e/%h/%Y"); open FILE,">$ARGV"; while(<DATA>){ my @tab_delimited_array = split(/\t/,$_); $tab_delimited_array =~ s/^\ =~ s/^\-//; my $converted_date =... (2 Replies)
Discussion started by: sandy1028
2 Replies

9. Shell Programming and Scripting

can we optimize this command

can we optimize this command ? sed 's#AAAA##g' /study/i.txt | sed '1,2d' | tr -d '\n\' > /study/i1.txt; as here i am using two files ...its overhead..can we optimise to use only 1 file sed 's#AAAA##g' /study/i.txt | sed '1,2d' | tr -d '\n\' > /study/i.txt; keeping them same but it... (9 Replies)
Discussion started by: crackthehit007
9 Replies

10. Shell Programming and Scripting

optimize the script

Hi, I have this following script below. Its searching a log file for 2 string and if found then write the strings to success.txt and If not found write strings to failed.txt . if one found and not other...then write found to success.txt and not found to failed.txt. I want to optimize this... (3 Replies)
Discussion started by: amitrajvarma
3 Replies
Login or Register to Ask a Question

Featured Tech Videos