Getting required fields from a text file in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting required fields from a text file in UNIX
# 1  
Old 06-29-2009
Getting required fields from a text file in UNIX

My data is something like as shown below. Out of this i want the details of alarms (ex: 1947147711,1947147081......) and the fields( ex :sw=tacmwafabb9:shelf=1:slot=5-2:pport=2)
Once i have these details separated, i want the count of these excluding the duplicates. What is the best possible way to get this?


sample data:
Code:
(06/25/2009 00:03:35.674)( No default rules found for alarm:1947147711
(06/25/2009 00:03:35.676)(:ERROR) No rule found for alarm: sw=tacmwafabb9:shelf=1:slot=5-2port=2
(06/25/2009 00:03:42.198)( No default rules found for alarm:1947147081
(06/25/2009 00:03:42.200)(:ERROR) No rule found for alarm: sw=mdfdor33dsm:slot=9:pport=1:channel=1:lport=1
(06/25/2009 00:03:51.026)(:) No default rules found for alarm:1947147493
(06/25/2009 00:03:51.028)(:ERROR) No rule found for alarm: sw=lscrnmmabb7:shelf=1:slot=13-2:pport=6
(06/25/2009 00:03:53.961)(:) No default rules found for alarm:1947147711

Output required :
Code:
Alarm id Port
1947147711 sw=tacmwafabb9:shelf=1:slot=5-2:pport=2
1947147081 sw=mdfdor33dsm:slot=9:pport=1:channel=1:lport=1
1947147493 sw=lscrnmmabb7:shelf=1:slot=13-2:pport=6


Last edited by otheus; 06-30-2009 at 06:17 AM.. Reason: codetags
# 2  
Old 06-30-2009
Try this..

cat <input-file> | awk '{print $8}'
# 3  
Old 06-30-2009
It's a little more complex than that, Jay,
Code:
awk '/alarm:[0-9]+$/ { split($NF,tmp,":");code=tmp[2]; } /No rule found for alarm: / { print code,$NF; }'

Once you have that output in, say "report.txt", you can get the "count" like this:
Code:
sort report.txt | uniq -c | sort -n


Last edited by otheus; 07-01-2009 at 11:53 AM.. Reason: fixed bug. want second field., not first
# 4  
Old 06-30-2009
It worked..Thanks a lot Jay :-)
# 5  
Old 06-30-2009
Use this too..

cat <input-file> | awk '{print $8}' | paste - -
# 6  
Old 06-30-2009
Otheus,

Your code is very helpful too...But i am not getting the details of alarms like 1947147711 and so on..
Please help.

---------- Post updated at 08:36 AM ---------- Previous update was at 08:34 AM ----------

i am getting details like this

Code:
alarm sw=mdfd33b0:shelf=.:slot=6:pport=4
alarm sw=scdlazmadsn:slot=7:pport=1:channel=3:lport=1
alarm sw=mplsmndtbbg:shelf=1:slot=9-1:pport=7
alarm sw=tcsnazmadsr:slot=8:pport=1:channel=25:lport=1
alarm sw=desmiadtbb4:shelf=1:slot=11-1:pport=1

Need alarm id's too.

Last edited by vgersh99; 06-30-2009 at 09:37 AM.. Reason: code tags, PLEASE!
# 7  
Old 06-30-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

2. Shell Programming and Scripting

Rearrange fields of delimited text file

I want to rearrange the fields of delimited text file after sorting first line (only): input file: a_13;a_2;a_1;a_10 13;2;1;10 the result should be: a_1;a_2;a_10;a_13 1;2;10;13 any help would be appreciated andy (20 Replies)
Discussion started by: andy2000
20 Replies

3. Shell Programming and Scripting

How to identify varying unique fields values from a text file in UNIX?

Hi, I have a huge unsorted text file. We wanted to identify the unique field values in a line and consider those fields as a primary key for a table in upstream system. Basically, the process or script should fetch the values from each line that are unique compared to the rest of the lines in... (13 Replies)
Discussion started by: manikandan23
13 Replies

4. Shell Programming and Scripting

How to Modify a file content in UNIX and sort for only required fields ?

I have the below contents in a file after making the below curl call curl ... | grep -E "state|Rno" | paste -sd',\n' | grep "Disconnected" > test "state" : "Disconnected",, "Rno" : "5554f1d2" "state" : "Disconnected",, "Rno" : "10587563" "state" : "Disconnected",, "Rno" :... (2 Replies)
Discussion started by: Vaibhav H
2 Replies

5. Shell Programming and Scripting

Parse file for fields and specific text

I have a file of ~500,000 entries in the following: file.txt chr1 11868 12227 ENSG00000223972.5 . + HAVANA exon . gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type... (17 Replies)
Discussion started by: cmccabe
17 Replies

6. Shell Programming and Scripting

How to search for blank fields in a text file from a certain position?

Sample txt file : OK00001111112| OK00003443434|skjdaskldj OK32812983918|asidisoado OK00000000001| ZM02910291029|sldkjaslkjdasldjk what would be the shell script to figure out the blank space (if any) after the pipe sign? (4 Replies)
Discussion started by: chatwithsaurav
4 Replies

7. Shell Programming and Scripting

Getting required fields from a test file in required fromat in unix

My data is something like shown below. date1 date2 aaa bbbb ccccc date3 date4 dddd eeeeeee ffffffffff ggggg hh I want the output like this date1date2 aaa eeeeee I serached in the forum but didn't find the exact matching solution. Please help. (7 Replies)
Discussion started by: rdhanek
7 Replies

8. Shell Programming and Scripting

how to convert fields from a text file to excel columns

i have this file which has the following contents: ,-0.3000 ,-0.3000 ,-0.3000 ,-0.9000 ,-0.9000 ,-0.9000 i would like to get this: -0.3-0.9-0.3-0.9-0.3-0.9 so far i am trying: awk '{for(i=1; i<=NF; i++) {printf("%f\n",$i)}}' test1 > test2 any help... (4 Replies)
Discussion started by: npatwardhan
4 Replies

9. UNIX for Dummies Questions & Answers

extract fields from text file using delimiter!!

Hi All, I am new to unix scripting, please help me in solving this assignment.. I have a scenario, as follows: 1. i have a text file(read1.txt) with the following data sairam,123 kamal,122 etc.. 2. I have to write a unix... (6 Replies)
Discussion started by: G.K.K
6 Replies

10. Shell Programming and Scripting

Need a Help with sort a text file with some fields

Ive got a file called listacdrs with this structure: 01/09/2006 12:13 p.m. 1.046.528 CF0155.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0156.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0157.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0158.DAT 01/09/2006 12:14 p.m. ... (3 Replies)
Discussion started by: alexcol
3 Replies
Login or Register to Ask a Question