ksh script to process grep output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh script to process grep output
# 1  
Old 06-06-2010
ksh script to process grep output

Hi,

I would like to know how can i pipe the following output of grep into a predefined output format

This is the output of the grep command grep record *.txt | sort -r
Code:
2010-04-28-11-12-21.txt:C The user has created a record
2010-04-29-10-18-41.txt:U The user has updated a record
2010-04-30-10-19-31.txt:D The user has deleted a record

I would like to know how can i extract the the file name and append words to the respective letter after the colon. eg. reate to the letter C

Desired output
Code:
2010-04-28-11-12-21.txt:Create
2010-04-29-10-18-41.txt:Update   
2010-04-30-10-19-31.txt:Delete

Thank you.
# 2  
Old 06-06-2010
Try appending this to your existing command:
Code:
| sed 's/ The user has [cud]\(.....\).*/\1/'


Last edited by pseudocoder; 06-06-2010 at 10:20 AM..
# 3  
Old 06-06-2010
Code:
grep record *.txt | sort -r | 
  awk '/U$/ {a="pdate"} /C$/ {a="reate"} /D$/ {a="elete"} {$0s= $0 a; print}'

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 06-06-2010
Another awk solution ....
Code:
BEGIN { FS="[ :]" }
$2=="C"  { printf "%s:Create\n",  $1 }
$2=="U"  { printf "%s:Update\n",  $1 }
$2=="D"  { printf "%s:Delete\n",  $1 }

# 5  
Old 06-06-2010
Quote:
Originally Posted by jim mcnamara
Code:
grep record *.txt | sort -r | 
  awk '/U$/ {a="pdate"} /C$/ {a="reate"} /D$/ {a="elete"} {$0s= $0 a; print}'

I got a syntax error while running this. What could be the problem? i'm not familiar with awk command. thank you.
# 6  
Old 06-06-2010
A tweaked version of jim's awk:
Code:
awk '{$0=$1} /U$/ {a="pdate"} /C$/ {a="reate"} /D$/ {a="elete"} {print $0a}'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 06-06-2010
Quote:
Originally Posted by alister
A tweaked version of jim's awk:
Code:
awk '{$0=$1} /U$/ {a="pdate"} /C$/ {a="reate"} /D$/ {a="elete"} {print $0a}'

Regards,
Alister
Thanks, this works perfectly fine.

If I want to print the original line in cases when the letters C,U,D is not found

Code:
2010-04-30-10-19-31.txt:X The user has executed a record

how should i look at it?

i believe it's got something to do with the regex. can i just add this rule in?
Code:
\b(?:(?!C|U|D)\w)+\b {print $0}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Line break in sqlplus output through ksh script

Hi, I am new to shell script programming. I have written a ksh script to run the sql File placed in server directory and spool the output in destination directory. Below Command: $ORACLE_HOME/bin/sqlplus -s $ora_uid @$sqlfile_loc$testquery.sql > $opfiledirectory It is generating the output... (6 Replies)
Discussion started by: Sumit Arora
6 Replies

2. Shell Programming and Scripting

Grep for process in a ksh script

Hi I'm trying to catch a particular process (XYZ) running using a ksh script. Like So.. VarPS=`ps -ef |grep XYZ |grep -v grep` However this seems to find the process of the script itself - not the process 'XYZ' Asked in Error - I found my own typo... thanks anyway Skrynesaver (1 Reply)
Discussion started by: Mudshark
1 Replies

3. Shell Programming and Scripting

ksh script to check if certain process is running

I have to kill the process "test" for a maintenance I do but want the script to check when it comes back up. I can get what I want when I run this while loop: while true;do ps -ef | grep test | grep -v grep | sed -e 's/^*//';sleep 60;done but I want the script to do it for me and as soon as... (6 Replies)
Discussion started by: seekryts15
6 Replies

4. UNIX for Dummies Questions & Answers

ksh script - not getting output from ls

Hi! I'm a complete noob, and I'm trying to write a little script that takes a directory pathname as input from the CL, checks whether it exists, and if not creates it, then shows it worked using ls. Everything works beautifully except in the first instance, wherein the directory is created, ls... (10 Replies)
Discussion started by: sudon't
10 Replies

5. Shell Programming and Scripting

Script in KSH to check if a process its up or down.

Hi all! Im working on a simple script in KSH (just started) to check if a process its up or down, but im kind of lost with the following error. Script: #!/usr/bin/ksh psup=$(ps -ef | grep sftp | egrep -v grep | wc -l) if ($psup > 0); then echo "Process SFTP running" else ... (6 Replies)
Discussion started by: ARSport
6 Replies

6. Shell Programming and Scripting

Help with ksh script to display output with specific contents

This is Input - starts with Storage Group Name and ends with Shareable and the loop continues all I need is Storage group name and Alu numbers in the below output format requested. Storage Group Name: abcd Storage Group UID: 00:00:000:00:0:0:0 HBA/SP Pairs: HBA UID ... (6 Replies)
Discussion started by: maddysa
6 Replies

7. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

8. Shell Programming and Scripting

Ksh Script to get the start minute of n number of process

How can i write a script.? which lists all X process and gets the start minute of each of them. thanks (1 Reply)
Discussion started by: Anteus
1 Replies

9. Shell Programming and Scripting

ksh: cmd output to input of another script

I want to write a script in KSH that takes the output of one command and redisplays it. Something like: while true do read inpt date +"%I:%M:%S %p <-> $inpt" done and then some how get the output of the ping command to redirect to the input of this script. does that make sense? (2 Replies)
Discussion started by: IMTheNachoMan
2 Replies

10. Shell Programming and Scripting

script ksh/awk/grep

How can I extract the 9th line of a text file. thanks vm.:confused: (2 Replies)
Discussion started by: hoang
2 Replies
Login or Register to Ask a Question