LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-22-2014, 10:09 PM   #1
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Rep: Reputation: Disabled
How to write below code in simpler way


Hi,

I have a below piece of code.
Code:
for JOB in "${JBLIST_FULL[@]}"
do
jb_rtv_date1=$(grep -A3 "$JOB" "$RTVIEW" | tail -1 | tr '[<>]' ' '| awk  '{print $5}')
jb_rtv_time1=$(grep -A3 "$JOB" "$RTVIEW" | tail -1 | tr '[<>]' ' '| awk  '{print $6}')
jb_rtv_status=$(grep -A3 "$JOB" "$RTVIEW" | tail -1 | tr '[<>]' ' '|awk -F ' ' '{ print $2,$3 }')
jb_rtv_day=$(grep -A3 "$JOB" "$RTVIEW" | tail -1 | tr '[<>]' ' '| awk  '{print $4}')
jb_rtv_hour=$(grep -A3 "$JOB" "$RTVIEW" | tail -1 | tr '[<>]' ' '| awk  '{print $6}'|cut -c1-2)
jb_rtv_min=$(grep -A3 "$JOB" "$RTVIEW" | tail -1 | tr '[<>]' ' '| awk  '{print $6}'|cut -c4-5)

done
but those grep|tail|tr|awk|cut chains are causing a huge overload because they are executing 6 times inside a for loop (that means about 700 forked processes, a lot of pipes .... ) -can we make this code much simpler and reduce overload?
 
Old 02-23-2014, 02:54 AM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,461

Rep: Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368Reputation: 1368
The likely answer is yes, by reading the contents of $RTVIEW into a bash array and using bash parameter expansions in place of the grep, tail, tr, awk and cut calls.

To help you, it would be good to see your attempt at a solution as well as examples of input and desired output.
 
Old 02-23-2014, 02:55 AM   #3
DrunkenBeetle
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Rep: Reputation: 0
How to write below code in simpler way

For starters the "grep ¦ tail ¦ tr" chain is the same for each of those 7 calls, so the output will be the same. In that case just store the output to a variable and use that as the input for those awk commands.

what is the data you are processing anyway? probably don't need awk if its something basic.

on the train so can't help much sorry
 
Old 02-23-2014, 03:16 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
My first thought is that awk can do all the necessary manipulating that your grep|tail|tr|awk|cut commands do.
Not only do all 6 use the same intro commands but 3 of them refer to the same field in the awk part ... so all in all quite redundant

I would generally say that without seeing a snippet of the data it is difficult to provide more feedback.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write UI-independent code? MTK358 Programming 10 01-12-2011 11:35 AM
Python newbie - a better way to write some code. Getafix Programming 2 06-25-2009 10:22 PM
how to write code with the better quality lordofring Programming 13 10-30-2005 02:48 AM
Write to code segment Earp Programming 4 04-22-2004 02:49 PM
i can't write C++ code in glade amr_azima Programming 1 03-21-2002 03:43 PM


All times are GMT -5. The time now is 05:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration