Extract specific pattern from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract specific pattern from a file
# 1  
Old 09-22-2008
Extract specific pattern from a file

Hi All,

I am a newbie to Shell Scripting.

I have a File

The Server Name
XXX002
-------------------------
2.1 LAPD

Iface Id Link MTU Side
ecc_3_1 4 Up 512 User
ecc_3_2 5 Up 512 User


The Server Name
XXX003
-------------------------
2.1 LAPD

Iface Id Link MTU Side
ecc_3_1 4 Up 512 User
ecc_3_2 5 down 512 User

I want to seperate All the servers which have a Id "Down" status

Like

The Server Name
XXX003
-------------------------
2.1 LAPD

Iface Id Link MTU Side
ecc_3_1 4 Up 512 User
ecc_3_2 5 down 512 User

Seperated from the file.

How can I do this with Shell Scripting ?

Please help me on the same.
# 2  
Old 09-22-2008
Try
Code:
awk '/^The/ {getline; s=$1} /[Dd]own/ {print s}' infile

# 3  
Old 09-22-2008
Hi zaxxon,

Thanks for the reply.

It displays only the server name which has got a link down status

But I want the whole Server details,

The Server Name
XXX003
-------------------------
2.1 LAPD

Iface Id Link MTU Side
ecc_3_1 4 Up 512 User
ecc_3_2 5 down 512 User

Please help me on the same

Thanks again,

Best Regards,

Athreya
# 4  
Old 09-22-2008
try something like this
Code:
awk 'disp(var) { if (match( var ~= /^The/) )
                      {if (index(save,"down")>0) { print save}
                        else {save=""}
                    } 
                    else {save=sprintf("%s%s\n", save, var)}
               }
      {disp($0) ; continue }' oldfile > newfile

# 5  
Old 11-12-2008
Hi All,

Sorry for the delay in replying

I achieved the task by

awk '/The server Name/ {n++} {print > f n}' f=/export/home/admin/op /export/home/admin/result.txt;
for i in `ls -lrt /export/home/admin/ | awk '{print $9}' | grep op`;
do
grep down $i

if [ $? -eq 0 ]

then

echo $i >> /export/home/admin/down

else

echo $i >> /export/home/admin/up

fi

done;

cat /export/home/admin/down | while read line

do

cat $line >> /export/home/admin/result$DATE.txt

done

rm /export/home/admin/op*

But when "result.txt" goes too large, awk fails with the error "Too many Files"

Some solution said to use "nawk"

Please help me to convert this to nawk

Regards,

Athreya
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

3. Shell Programming and Scripting

Extract a specific string from a file

Hi, I have a file whose contents are as follows. 2013-03-08/15:09:20.134 INFO 00000000-00000000 0034 09700400 CON_IN SessionID:ED5E1400-4805-85E2-17B2-5BE45684886A Connection ID:ED5E1400-4805-68F1-BB1D-F06496BCF910 TO:<sip:51234999@10.239.94.146:5060 FROM:<sip:9302280716@97.208.31.7:51024... (2 Replies)
Discussion started by: SunilB2011
2 Replies

4. Shell Programming and Scripting

Extract Specific pattern - log file

Hello everyone, I am on AIX (6.1). I can only use shell (ksh) script. I can't do this on my own, so will do my best to explain my needs.I also do not know what is the best idea to make it work, so here is what I am thinking, but I may wrong. I need help to extract info on... (3 Replies)
Discussion started by: Aswex
3 Replies

5. Shell Programming and Scripting

Extract specific lines from a file

Hi, I have a file which contains DDL statements- CREATE TABLE, CREATE INDEX, ALTER TABLE etc. I have to only pick CREATE TABLE statements from the file- Source : ---------------------------------------------- --DDL for table abc -------------------------------------------- CREATE TABLE... (4 Replies)
Discussion started by: newb
4 Replies

6. Shell Programming and Scripting

extract specific line if the search pattern is found

Hi, I need to extract <APPNUMBER> tag alone, if the <college> haas IIT Chennai value. college tag value will have spaces embedded. Those spaces should not be suppresses. My Source file <Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT ... (3 Replies)
Discussion started by: Sekar1
3 Replies

7. Shell Programming and Scripting

Extract specific content from a file

My input file: >sequence_1 ASSSSSSSSSSSDDDDDDDDDDDCCCCCCC ASDSFDFFDFDFFWERERERERFSDFESFSFD >sequence_2 ASDFDFDFFDDFFDFDSFDSFDFSDFSDFDSFASDSADSADASD ASDFFDFDFASFASFASFAFSFFSDASFASFASFAFS >sequence_3 VEDFGSDGSDGSDGSDGSDGSDGSDG dDFSDFSDFSDFSDFSDFSDFSDFSDF SDGFDGSFDGSGSDGSDGSDGSDGSDG My... (22 Replies)
Discussion started by: patrick87
22 Replies

8. Shell Programming and Scripting

to extract specific value in a file

Hi Friends, I have a file with the following values.. xyz.txt,12345.xml abc.txt,04567.xml cde.txt,12134.xml I would like to extract all the 2nd column values like 12345 04567 12134 Please advice!! (3 Replies)
Discussion started by: techmoris
3 Replies

9. Shell Programming and Scripting

Need to extract specific pattern from logfile

Log File: Attempting to contact (DESCRIPTION=(SOURCE_ROUTE=OFF)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname1.com)(PORT=1521)))(CONNECT_DATA=(SID=database1)(SRVR=DEDICATED))) Attempting to contact... (2 Replies)
Discussion started by: techychap
2 Replies

10. HP-UX

extract field of characters after a specific pattern - using UNIX shell script

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could... (2 Replies)
Discussion started by: jansat
2 Replies
Login or Register to Ask a Question