how to extract info from a file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to extract info from a file using awk
# 1  
Old 01-13-2009
how to extract info from a file using awk

Dear all

I have a file call interfaces.txt

Filename: interfaces.txt
Quote:
ABC_DB_001
master tcp ether hostname1 20901
query tcp ether hostname1 20901

ABC_DB_002
master tcp ether hostname2 20903
query tcp ether hostname2 20903

ABC_DB_003
master tcp ether hostname3 20905
query tcp ether hostname3 20905
How can I extract the information at below?
ABC_DB_001 hostname1 20901
ABC_DB_002 hostname2 20903
ABC_DB_003 hostname3 20905

Currently I am using a very stupid method
Code:
grep ^ABC interfaces.txt > name.txt
grep "master tcp" interfaces.txt > host.txt

total_line=`wc -l name.txt`
i=1
while i < $total_line
do
   name=`head -$i name.txt | tail -1`
   host=`head -#i host.txt | tail -1 | awk '{print $4}`
   port =`head -#i host.txt | tail -1 | awk '{print $5}` 
   echo "$name $host $port"
done

How can I do this using awk? Please give me some idea. Thank you.

Valentino
# 2  
Old 01-13-2009
Code:
awk '{print$1,$(NF-1),$NF}' RS= interfaces.txt

# 3  
Old 01-13-2009
Quote:
Originally Posted by radoulov
Code:
awk '{print$1,$(NF-1),$NF}' RS= interfaces.txt

Thank you for your help

I google to study what the RS= is. It is to breaks up the line into fields.

However, if my interfaces.txt like this (do not have blank line seperated). How to do then?

Quote:
LIS_SIM_UX7
master tcp ether dc1ibm23.server.ha.org.hk 26007
LIS_SIM_UX7_SB
master tcp ether dc1ibm22.server.ha.org.hk 26007
LIS_SIM_UX9
master tcp ether dc1ibm23.server.ha.org.hk 26009
LIS_SIM_UX9_SB
master tcp ether dc1ibm22.server.ha.org.hk 26009
LIS_SIM_ST1
master tcp ether cdcibm26.server.ha.org.hk 35501
LIS_SYB_UT1
master tcp ether dc1ibm23.server.ha.org.hk 61199
Many thanks
Valentino
# 4  
Old 01-13-2009
Use nawk or /usr/xpg4/bin/awk on Solaris.

Code:
awk '{printf"%s",(NF==1?$1FS:$(NF-1)FS$NF RS)}' interfaces.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with awk to extract additional info

Hi I use multipath linux command to get LUNs info and find out if any failed. # multipath -ll >/tmp/mpfail # cat /tmp/mpfail multipath.conf line 109, invalid keyword: user_friendly_names multipath.conf line 153, invalid keyword: user_friendly_names multipath.conf line 193, invalid... (4 Replies)
Discussion started by: prvnrk
4 Replies

2. Shell Programming and Scripting

Extract info and do algebra on it by sed or awk

Hello everyone, I need to extract some information from a csv file and further need to do some algebraic calculations on those information and then to throw the result in a new file. Here is a sample from my data.csv file; Col1,Col2,Col3,Col4,Col5,Col6,Col7... (19 Replies)
Discussion started by: hayreter
19 Replies

3. UNIX for Advanced & Expert Users

How to extract info from text file between the tags

Hi, I have a text file with member information... B]Name is in H1 tag Title is in H2 tag Email is in <a id="ctl00_ContentPlaceHolder3_repeaterItems_ctl01_lbnEmailMe" href="javascript:__doPostBack('ctl00$ContentPlaceHolder3$repeaterItems$ctl01$lbnEmailMe','')">someone@company.com</a> Location:... (6 Replies)
Discussion started by: igurv
6 Replies

4. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

5. Shell Programming and Scripting

How to extract the day of the year and use that info to copy a file remotely

Hello, Thank you in advance for helping a newbie who is having great trouble with this simple task. I'm allowed to copy one file remotely each night due to bandwidth restrictions. A new file gets generated once a day, and I need to copy the previous day's file. Here is what I'd like to do:... (1 Reply)
Discussion started by: tmozdzen
1 Replies

6. Shell Programming and Scripting

how to extract the info in the tag from a xml file

Hi All, Do anyone of you have any idea how to extract each<info> tag to each different file. I have 1000 raw files, which come in every 15 mins.( I am using bash) I have tried my script as below, but it took hours to finish, which is inefficiency. perl -n -e '/^<info>/ and open FH,">file".$n++;... (2 Replies)
Discussion started by: natalie23
2 Replies

7. Shell Programming and Scripting

Using AWK BEGIN to extract file header info into variables

Hi Folks, I've searched for this for quite a while, but can't find any solution - hope someone can help. I have various files with standard headers. eg. <HEADER> IP: 1.2.3.4 Username: Joe Time: 12:00:00 Date: 23/05/2010 </HEADER> This is a test and this part can be any size... (6 Replies)
Discussion started by: damoske
6 Replies

8. Shell Programming and Scripting

How to pull info under headers in file(awk,grep,while loop)

below is an extract from my file and I am trying to use Awk and grep and a while loop to pull infomation from under neath "HBA WWN=".HBA WWN=" reoccurs all over the file but the 100000c.....number are unique and I want to be able to pull and reference specifi information under this header ever time... (2 Replies)
Discussion started by: kieranfoley
2 Replies

9. Shell Programming and Scripting

Extract info from log file and compute using time date stamp

Looking for a shell script or a simple perl script . I am new to scripting and not very good at it . I have 2 directories . One of them holds a text file with list of files in it and the second one is a daily log which shows the file completion time. I need to co-relate both and make a report. ... (0 Replies)
Discussion started by: breez_drew
0 Replies

10. AIX

Extract info

Anyone have a better idea to automate extraction of info like ... "uname" "ifconfig" "ps efl" "netstat -ao" etc. from several hundred aix, solaris, red hat boxes? without logging into each box and manually performing these tasks and dumping them to individual files? thanks for any input (1 Reply)
Discussion started by: chm0dvii
1 Replies
Login or Register to Ask a Question