Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

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


I have a below piece of code.
for JOB in "${JBLIST_FULL[@]}"
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)

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
Senior Member
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,685

Rep: Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572Reputation: 1572
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
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
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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.


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 > Forums > Linux Forums > Linux - Newbie

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

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