Need help in awk scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in awk scripting
# 1  
Old 07-02-2013
Network Need help in awk scripting

Hi

I am beginner of shell/AWK scripting , can you please help me in select particular column and column between two pattern from a multiple column file.

file1.txt
Code:
number status  date1  date2              description                                                         category                                     type                 service  assigned_to     reported_to  envioment 
=====  ====   ===      =====            =============                                             =============                        =====             =======  =========     ======== ======
12345 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading init Oracle Services - Trouble Tk Ticket, Auto √ DBA_SERVICE chadda,Deepak kumar  DBA PROD
23456 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading Sizing Oracle Services - Trouble Tk Ticket, Auto √ DBA_SERVICE chadda,Deepak kumar  DBA TEST
45678 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading DBMS info Oracle Services - Trouble Tk Ticket, Auto √  DBA_SERVICE chadda,Deepak kumar  DBA QUALITY
24578 open 28/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading Trig/Proc/Syn Oracle Services - Trouble Tk Ticket, Auto √  DBA_SERVICE shama,Vijay Kumar  DBA PROD
45890 open 28/06/13 28/06/13  nl21a00is-centerdb001:testingQA:FSFO has configuration errors Oracle Services - Trouble Tk Ticket, Auto √  DBA_SERVICE Reddy,Ajay rao   DBA PROD

I want to select " number, description, assigned_to "


description field end before category field start "Oracle Service-Trouble Tk"
assigned_to field is between two pattern DBA_SERVICE and DBA e.g. chadda,Deepak kumar

desire out out

out.txt
Code:
12345   nl21a00is-centerdb001:ncdbareq:Error in loading init  chadda,Deepak kumar  
23456   nl21a00is-centerdb001:ncdbareq:Error in loading Sizing  chadda,Deepak kumar  
45678   nl21a00is-centerdb001:ncdbareq:Error in loading DBMS info chadda,Deepak kumar  
24578   nl21a00is-centerdb001:ncdbareq:Error in loading Trig/Proc/Syn shama,Vijay Kumar 
45890   nl21a00is-centerdb001:testingQA:CSSO has configuration errors  Reddy,Ajay rao

i want to select column1 and column5 (all description column before "category" column and third last column(which is in-between to pattern DBA_SERVICE and DBA)

Last edited by Scott; 07-02-2013 at 03:18 AM.. Reason: Please use code tags
# 2  
Old 07-02-2013
Try this one..

Code:
awk '{print $1,$5,$6,$7,$8,$18,$19}' file1.txt


Last edited by Scott; 07-02-2013 at 03:19 AM.. Reason: Please use code tags
# 3  
Old 07-02-2013
will this work?

Code:
 
sed 's/\(.*\)Oracle Services .* DBA_SERVICE\(.*\)DBA.*/\1\2/g' filename|awk 'NR>2{$2=$3=$4="";print}'

# 4  
Old 07-02-2013
Quote:
Originally Posted by millan
Try this one..

Code:
awk '{print $1,$5,$6,$7,$8,$18,$19}' file1.txt

Code:
Vijay-G:~ vijaygarhewal$ awk '{print $1,$5,$6,$7,$8,$18,$19}' file1.txt
12345 nl21a00is-centerdb001:ncdbareq:Error in loading init chadda,Deepak kumar
23456 nl21a00is-centerdb001:ncdbareq:Error in loading Sizing chadda,Deepak kumar
45678 nl21a00is-centerdb001:ncdbareq:Error in loading DBMS DBA_SERVICE chadda,Deepak
24578 nl21a00is-centerdb001:ncdbareq:Error in loading Trig/Proc/Syn shama,Vijay Kumar
45890 nl21a00is-centerdb001:testingQA:FSFO has configuration errors Reddy,Ajay rao

but Its not containing all the description column...In third line description field contain "nl21a00is-centerdb001:ncdbareq:Error in loading DBMS info"

the "info" is missing in this output.

I have already tried this ...the description column may contain 3 or 4 or 5 or 6 or 7....or 10 field but It is sure new column will start with "Oracle Services - Trouble Tk"

so we need to read all the field of description column and stop reading once we get "Oracle Services - Trouble Tk" and print.

can you please provide syntax for this..

---------- Post updated at 08:05 AM ---------- Previous update was at 08:01 AM ----------

Quote:
Originally Posted by vidyadhar85
will this work?

Code:
 
sed 's/\(.*\)Oracle Services .* DBA_SERVICE\(.*\)DBA.*/\1\2/g' filename|awk 'NR>2{$2=$3=$4="";print}'


This is not giving any output...
Code:
Vijay-G:~ vijaygarhewal$ cat file1.txt
12345 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading init Oracle Services - Trouble Tk Ticket, Auto √ DBA_SERVICE chadda,Deepak kumar  DBA PROD
23456 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading Sizing Oracle Services - Trouble Tk Ticket, Auto √ DBA_SERVICE chadda,Deepak kumar  DBA TEST
45678 open 27/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading DBMS info Oracle Services - Trouble Tk Ticket, Auto √  DBA_SERVICE chadda,Deepak kumar  DBA QUALITY
24578 open 28/06/13 28/06/13  nl21a00is-centerdb001:ncdbareq:Error in loading Trig/Proc/Syn Oracle Services - Trouble Tk Ticket, Auto √  DBA_SERVICE shama,Vijay Kumar  DBA PROD
45890 open 28/06/13 28/06/13  nl21a00is-centerdb001:testingQA:FSFO has configuration errors Oracle Services - Trouble Tk Ticket, Auto √  DBA_SERVICE Reddy,Ajay rao   DBA PROD
Vijay-G:~ vijaygarhewal$ 
Vijay-G:~ vijaygarhewal$ 
Vijay-G:~ vijaygarhewal$ sed 's/\(.*\)Oracle Services .* DBA_SERVICE\(.*\)DBA.*/\1\2/g' file1.txt|awk 'NR>2{$2=$3=$4="";print}'


Last edited by Scott; 07-02-2013 at 11:22 AM.. Reason: Code tags, please...
# 5  
Old 07-02-2013
Please use code tags for posting code fragments or data samples.

Try this:
Code:
awk 'NR>2{n=$1;gsub(/.*[0-9]*\/[0-9]*\/[0-9]* |Oracle.*_SERVICE|DBA[ ].*/,X);print n,$0}' file

# 6  
Old 07-02-2013
Quote:
Originally Posted by Yoda
Please use code tags for posting code fragments or data samples.

Try this:
Code:
awk 'NR>2{n=$1;gsub(/.*[0-9]*\/[0-9]*\/[0-9]* |Oracle.*_SERVICE|DBA[ ].*/,X);print n,$0}' file

This looks good..can you explain me the syntax..

please help me to understandb gsub function..

Last edited by vijay_rajni; 07-02-2013 at 11:16 AM..
# 7  
Old 07-02-2013
Code:
awk '
NR > 2 {                                                        # Skip first 2 lines
 n = $1                                                          # Set n = 1st field
 # Substitute every chars until date with null (.*[0-9]*\/[0-9]*\/[0-9]*)
 #12345 open 27/06/13 28/06/13 nl21a00is-centerdb001:ncdbareq:Error in loading init Oracle Services - Trouble Tk Ticket, Auto √ DBA_SERVICE chadda,Deepak kumar DBA PROD

 # Substitute Oracle followed by zero or more occurrence of any chars until _SERVICE (Oracle.*_SERVICE)
 #nl21a00is-centerdb001:ncdbareq:Error in loading init Oracle Services - Trouble Tk Ticket, Auto √ DBA_SERVICE chadda,Deepak kumar DBA PROD

 # Substitute DBA followed by space followed by zero or more occurrence of any chars (DBA[ ].*)
 #nl21a00is-centerdb001:ncdbareq:Error in loading init chadda,Deepak kumar DBA PROD

 gsub(/.*[0-9]*\/[0-9]*\/[0-9]* |Oracle.*_SERVICE|DBA[ ].*/,X)
 # Print n , $0
 print n,$0
        }
' file


Last edited by Yoda; 07-02-2013 at 01:49 PM.. Reason: commented
This User Gave Thanks to Yoda 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

New at scripting awk with variable

I am trying to write a shell script that will add a date to and awk command from the command prompt. here is the line I am having difficulties. zgrep -i playback $dir/$1-*-errors.out.gz |cut -d '|' -f 1,11,12,15 | awk -v start=${start} -v end=${end} -F '|' '{$1>=start && $1 <=end} {print $2... (7 Replies)
Discussion started by: infinity0880
7 Replies

2. Shell Programming and Scripting

awk Scripting

Hey guys, I want to get all the columns in this input file tab-delimited, i need to get the column send them to a variable. From there i could print them in shuffle and pick and select columns i want. Here is the input sample 2013/08/05 06:50:38:067 MINOR SiteScope ... (9 Replies)
Discussion started by: ryandegreat25
9 Replies

3. Shell Programming and Scripting

Need help with awk scripting

hi all, I am working on awk scripting.I have created two awk files and now have a requirement of replacing the contents of first file with some contents of second file. Please find below the two files created.File1 has 3 records and File2 has 4 records. cat File1 111,0165,CB21031251,0165,... (3 Replies)
Discussion started by: csrohit
3 Replies

4. Shell Programming and Scripting

Help with AWK and Scripting!

Hi, This is the first time I am working with awk and I am not familiar with any commands in it. But I managed to do most of my work just left with one more. Needing your help! I have to extract only the matrix (written within ) from a text file. For example: 1JTJ_0006_ACGC_NPNP_A_12_15.pdb ... (17 Replies)
Discussion started by: SriJit
17 Replies

5. Shell Programming and Scripting

Need help with awk scripting.

Hi, i am newbie to this site and hope to learn but problem is s but need help urgently. Plz pm me if you are good at this. Help will be appreciated. (11 Replies)
Discussion started by: Rookie80
11 Replies

6. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

7. Shell Programming and Scripting

awk scripting

Hi I have 2 files of large size( 10 Miilions) , i want to join 2 files based on some condition . for this taking lot of time and 100 cpu .i want to iterate the based on some value (like 1 lakh) I put the 2 files in the associative arrays . if the array limit reaches the 1 lach join the with... (2 Replies)
Discussion started by: kiranmosarla
2 Replies

8. Shell Programming and Scripting

Scripting via awk

Hi, I am trying to understand what is happening here, new to scripting: I have a couple of these, but if I knew what was going on in one I can figure out the rest: awk '/rpc-100083/ { $2 = "enable -r" } $3 ~ /.NOS99dtlogin/ { $t = $2; $2 = $3; $3 = $t } { print }' /var/svc/profile/upgrade... (2 Replies)
Discussion started by: ulemsee
2 Replies

9. Shell Programming and Scripting

AWK scripting

I have a text file in which the text has been divided into paragraphs (two line breaks or tab marks a new paragraph) and I want to make a script which output would delete line breaks within the paragraph and the different paragraphs would be separated by two line breaks. So, if my input file... (14 Replies)
Discussion started by: Muki101
14 Replies

10. UNIX for Dummies Questions & Answers

Awk scripting

Hi, I'm new to unix and i am kind of familiar with the basic commands. can anyone suggest some good books especially for AWK scripting and SHELL scripting thanks, Hari (2 Replies)
Discussion started by: rharee
2 Replies
Login or Register to Ask a Question