sed scripting help need


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed scripting help need
# 1  
Old 07-10-2009
sed scripting help need

hi all,

i want to display output of sar, whichever idle time is less than 30%..i want to add HI and BYE at the starting and ending of the line.

For an example:
sar

Linux 2.6.9-78.0.1.ELsmp (hostname) 07/10/2009

07:10:01 AM CPU %user %nice %system %iowait %idle
07:15:01 AM all 16.49 0.00 1.56 0.16 81.78
07:20:01 AM all 16.55 0.00 1.32 0.03 82.11
07:25:01 AM all 16.71 0.00 1.33 0.01 81.95
07:30:01 AM all 16.63 0.00 1.25 0.01 82.11
07:35:01 AM all 15.91 0.00 1.35 0.13 82.61
HI 07:40:01 AM all 16.69 0.00 1.23 0.05 22.02 BYE
07:45:01 AM all 16.64 0.00 1.44 0.22 81.70
07:50:01 AM all 17.05 0.00 1.37 0.01 81.57
07:55:01 AM all 17.01 0.00 1.37 0.01 81.61
08:00:01 AM all 16.70 0.00 1.35 0.04 81.92
08:05:01 AM all 15.97 0.00 1.43 0.01 82.59
08:10:01 AM all 16.72 0.00 1.39 0.04 81.85
08:15:01 AM all 16.99 0.00 1.54 0.01 81.47
08:20:01 AM all 16.74 0.00 1.36 0.01 81.90
HI 08:25:01 AM all 17.26 0.00 1.77 0.06 19.91 BYE
08:30:01 AM all 16.83 0.00 1.36 0.05 81.75
Average: all 16.34 0.00 1.36 0.09 82.22
# 2  
Old 07-10-2009
Code:
 
cat input | sed -e '1d' -e '$d' >input1
head -1 input >output
while read record
do
a=`echo $record | awk -F " " '{print $8}'`
if test $a -le 30
then
echo $record | sed -e 's/^/HI /' -e 's/$/ BYE/' >>output
else
echo $record >>output
fi
done<input1
tail -1 input >>output

# 3  
Old 07-10-2009
I hope awk will be easy to use .

something like this :
Code:
TESTBOX>echo "07:40:01 AM all 16.69 0.00 1.23 0.05 22.02" \
>  | awk '{ if ( substr($NF,1,index($NF,".")-1) <=30  ) {$0="Hi "$0" Bye"} print }'
Hi 07:40:01 AM all 16.69 0.00 1.23 0.05 22.02 Bye

TESTBOX>echo "07:40:01 AM all 16.69 0.00 1.23 0.05 32.02" \
> | awk '{ if ( substr($NF,1,index($NF,".")-1) <=30  ) {$0="Hi "$0" Bye"} print }'
07:40:01 AM all 16.69 0.00 1.23 0.05 32.02

# 4  
Old 07-10-2009
Hi Panyam,

I tried u r code:

echo "07:40:01 AM all 16.69 0.00 1.23 0.05 32.02" \
> > | awk '{ if ( substr($NF,1,index($NF,".")-1) <=30 ) {$0="Hi "$0" Bye"} print }'
-bash: syntax error near unexpected token `|'
# 5  
Old 07-10-2009
something wrong You did i think in execution :| , check one more extra ">" in the second line of the code you pasted.

do something like this
Code:
awk '{ if ( substr($NF,1,index($NF,".")-1) <=30  ) {$0="Hi "$0" Bye"} print }' your_file_name.txt

# 6  
Old 07-10-2009
It's probably easier in awk?

Code:
awk '($NF ~ /\./) && ($NF < 30) {$0 = "HI " $0 " BYE"} {print}' input_file
or
sar ... |  awk '($NF ~ /\./) && ($NF < 30) {$0 = "HI " $0 " BYE"} {print}'

 
 
07:10:01 AM CPU %user %nice %system %iowait %idle
07:15:01 AM all 16.49 0.00 1.56 0.16 81.78
07:20:01 AM all 16.55 0.00 1.32 0.03 82.11
07:25:01 AM all 16.71 0.00 1.33 0.01 81.95
07:30:01 AM all 16.63 0.00 1.25 0.01 82.11
07:35:01 AM all 15.91 0.00 1.35 0.13 82.61
HI 07:40:01 AM all 16.69 0.00 1.23 0.05 22.02 BYE
07:45:01 AM all 16.64 0.00 1.44 0.22 81.70
07:50:01 AM all 17.05 0.00 1.37 0.01 81.57
07:55:01 AM all 17.01 0.00 1.37 0.01 81.61
08:00:01 AM all 16.70 0.00 1.35 0.04 81.92
08:05:01 AM all 15.97 0.00 1.43 0.01 82.59
08:10:01 AM all 16.72 0.00 1.39 0.04 81.85
08:15:01 AM all 16.99 0.00 1.54 0.01 81.47
08:20:01 AM all 16.74 0.00 1.36 0.01 81.90
HI 08:25:01 AM all 17.26 0.00 1.77 0.06 19.91 BYE
08:30:01 AM all 16.83 0.00 1.36 0.05 81.75
Average: all 16.34 0.00 1.36 0.09 82.22


Last edited by Scott; 07-10-2009 at 11:27 AM..
# 7  
Old 07-10-2009
Hi Panyam,

its working now...thanks a lot!!!

---------- Post updated at 09:17 AM ---------- Previous update was at 08:25 AM ----------

hi scottn,

I tried u r code...
sar ... | awk '(NR > 1) && ($NF < 30) {$0 = "HI " $0 " BYE"} { print}'

the output is....

sar | awk '(NR > 1) && ($NF < 30) {$0 = "HI " $0 " BYE"} { print}'
Linux 2.6.9-78.ELsmp (lrtp68) 07/10/2009
HI BYEHI 12:00:01 AM CPU %user %nice %system %iowait %idle BYE
12:05:01 AM all 2.32 0.00 0.41 0.00 97.27
12:10:01 AM all 1.86 0.00 0.27 0.00 97.87
12:15:01 AM all 5.10 0.00 1.00 0.17 93.73
12:20:01 AM all 2.47 0.00 0.25 0.00 97.28
12:25:01 AM all 2.13 0.00 0.24 0.00 97.63
12:30:01 AM all 2.32 0.00 0.39 0.01 97.28
12:35:01 AM all 6.58 0.00 0.62 1.19 91.61
12:40:01 AM all 2.25 0.00 0.26 0.00 97.49
12:45:01 AM all 2.42 0.00 0.27 0.00 97.31
12:50:01 AM all 2.14 0.00 0.27 0.00 97.59
12:55:01 AM all 2.56 0.00 0.59 0.01 96.85
01:00:01 AM all 1.97 0.00 0.23 0.01 97.78
10:00:01 AM all 4.31 0.00 0.75 0.01 94.94
10:05:01 AM all 3.26 0.00 0.78 0.01 95.96
Average: all 3.36 0.00 0.50 0.02 96.11
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help shell scripting using awk or sed or other

I need to create a script to change a file depending of 3 conditions using a target as parameter... first condition <chamada> <numeroTerminalOriginador>CALLER</numeroTerminalOriginador> <imeiOriginador></imeiOriginador> <cgiPrimeiraErbOriginador></cgiPrimeiraErbOriginador>... (2 Replies)
Discussion started by: poulis
2 Replies

2. Shell Programming and Scripting

Need help with awk and sed scripting

I need help with sed and awk scripts to search for Symmetrix ID=000090009902 and then grep its child disk devices associated to the dead paths and display them only, so that those dead devices can be removed. test01:/#powermt display dev=all Pseudo name=hdiskpower0 Symmetrix ID=000090009902... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

3. Shell Programming and Scripting

sed newbie scripting assistance

Howdy folks, I'm trying to craft a log file summarisation tool for an application that creates a lot of duplicate entries with only a different suffix to indicate point of execution. I thought I'd gotten close but I'm clearly missing something. Here's a genericized version: A text_file... (3 Replies)
Discussion started by: mthespian
3 Replies

4. Shell Programming and Scripting

Need help with sed scripting

Hi expert, i need URGENT help in bash shell scripting using sed: i woud like to search for the word XMLRPC which is at theend of the line below in up2date file. Once found or match serverURL=https://redhat.com/XMLRPC replace with myserver.com like this ... (1 Reply)
Discussion started by: lamoul
1 Replies

5. Shell Programming and Scripting

Sed scripting issue

Hi all! I was wondering if anyone could help suggest some solutions to an issue i'm having using the Sed command. I'm still a relative novice at this, but slowly learning and enjoying the power of batch processing. I am using Sed to extract values from a .txt file containing hundreds of... (2 Replies)
Discussion started by: StudentFitz
2 Replies

6. Shell Programming and Scripting

Shell Scripting -- sed

Hi, In one of my scripts, I am using sed to do an expression replacement. The code in the script is as under sed "s|MY_INP_Lab=""|MY_INP_Lab="${2}"|" file1, where $2=xyz_abc_mbk The EXPECTED output is in file1, all the instances ofMY_INP_Lab="" shall be replaced by... (2 Replies)
Discussion started by: vivekmattar
2 Replies

7. Shell Programming and Scripting

sed or awk scripting help needed

hi all, for an example : df -k output shows: $ df -k Filesystem 1K-blocks Used Available Use% Mounted on /dev/cciss/c0d0p6 3099260 1117760 1824068 8% / /dev/cciss/c0d0p1 256666 18065 225349 8% /boot none 8219180 0 8219180 0% /dev/shm /dev/mapper/vglocal-home 1032088 245172 734488 26%... (7 Replies)
Discussion started by: raghur77
7 Replies

8. Shell Programming and Scripting

SED scripting select

Say I have a file 'example.txt' with these lines of code in it: hello:anddasd:cheese:gerg whatever:sdadsa:asdfasdfa:wwew hmmmm:something:gfhfhgf:sdasdas Question: 1. How would I write a script which is able to take all the words before the first ':'? 2. How would I write a script which is... (6 Replies)
Discussion started by: i_am_a_robot
6 Replies

9. Shell Programming and Scripting

scripting with awk and sed

hey all, i was just wondering if it was possible to to get data from user input , and parse it through sed to remove or add what that user has entered into a flat file? do i need awk ? any help is greatly appreciated ~shan2on (2 Replies)
Discussion started by: shan2on
2 Replies

10. UNIX for Dummies Questions & Answers

scripting help with touch and sed

Hello, I am trying to write a simple script to wake up every 300 seconds and look for a new file in a directory (e.g., "G102005"), remove the "G" from the file name, then print the new filename. Here's what I've got so far. I'm new at this so please forgive the errors or misuse of syntax. It... (14 Replies)
Discussion started by: alexkav
14 Replies
Login or Register to Ask a Question