Script to get required output from textfile | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Script to get required output from textfile

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-10-2013
surender reddy surender reddy is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 4 March 2013, 3:04 AM EST
Posts: 36
Thanks: 10
Thanked 0 Times in 0 Posts
Script to get required output from textfile

Hi

Iam running below script on one text file.


Code:
usr/xpg4/bin/awk 'BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/local/ {HOST=$1}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
' /Scripts/sub_report_$FILE>/Scripts/sub_final_report_.txt

the output is coming as below


IP
HTML Code:
HOST_NAME SUB 
10.238.48.1 [local]bgl-ras-bng-bge-01#cont 12853
10.238.48.33 [local]bgl-ras-bng-bge-02#cont 9527
10.238.48.193 [local]bgl-ras-bng-jnr-03#cont 13475
10.238.48.225 [local]bgl-ras-bng-jnr-04#cont 10753
but I need script to get below output

HTML Code:
IP HOST_NAME SUB 
10.238.48.1 bgl-ras-bng-bge-01 12853
10.238.48.33 bgl-ras-bng-bge-02 9527
10.238.48.193 bgl-ras-bng-jnr-03 13475
10.238.48.225 bgl-ras-bng-jnr-04 10753
can anybody help

tnx in advance.



Last edited by Scrutinizer; 02-11-2013 at 05:29 AM.. Reason: code tags
Sponsored Links
    #2  
Old 02-10-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 22 October 2014, 12:48 PM EDT
Location: India
Posts: 1,831
Thanks: 25
Thanked 525 Times in 504 Posts
Could you please paste a few lines from your input file for a sample?
Sponsored Links
    #3  
Old 02-10-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 23 October 2014, 9:45 AM EDT
Location: Aachen, Germany
Posts: 4,420
Thanks: 73
Thanked 1,079 Times in 1,018 Posts
What about adding sub(/\[local\]|#cont/, "") to your awk program?
    #4  
Old 02-10-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
Change

Code:
/local/ {HOST=$1}

to

Code:
/local/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}

.

Note that this is without looking at your input and just by looking at your output. It's always better to post input sample as balajesuri pointed out.

@RudiC, that's a sub . So, it will do only 1 replacement if possible.
Sponsored Links
    #5  
Old 02-10-2013
surender reddy surender reddy is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 4 March 2013, 3:04 AM EST
Posts: 36
Thanks: 10
Thanked 0 Times in 0 Posts
Hi

below are few lines from input file

HTML Code:
IP IS 10.238.48.225
spawn telnet 10.238.48.225
Trying 10.238.48.225...
Connected to 10.238.48.225.
Escape character is '^]'.

bgl-ras-bng-jnr-04

[bsnl.in]bgl-ras-bng-jnr-04#show sub sum all 
--------------------------------------------------------------------------------
Total=10066

Type            Authenticating          Active          Disconnecting
PPP                          0               0                      0
PPPoE                        2           10064                      1
DOT1Q                        0               0                      0
CLIPs                        0               0                      0
ATM-B1483                    0               0                      0
ATM-R1483                    0               0                      0
Mobile-IP                    0               0                      0
[bsnl.in]bgl-ras-bng-jnr-04#exit
Connection to 10.238.48.225 closed by foreign host.
IP IS 10.238.49.1
spawn telnet 10.238.49.1
Trying 10.238.49.1...
Connected to 10.238.49.1.
Escape character is '^]'.

chn-ras-bng-hdw-01

[local]chn-ras-bng-hdw-01#cont bsnl.in
[bsnl.in]chn-ras-bng-hdw-01#show sub sum all 
--------------------------------------------------------------------------------
Total=7567

Type            Authenticating          Active          Disconnecting
PPP                          0              19                      0
PPPoE                        4            7544                      5
DOT1Q                        0               0                      0
CLIPs                        0               0                      0
ATM-B1483                    0               0                      0
ATM-R1483                    0               0                      0
Mobile-IP                    0               0                      0
[bsnl.in]chn-ras-bng-hdw-01#exit
Connection to 10.238.49.1 closed by foreign host.
IP IS 10.238.49.33
spawn telnet 10.238.49.33
Trying 10.238.49.33...
Connected to 10.238.49.33.
Escape character is '^]'.
the modifed code is giving syntax error

code

HTML Code:
root@blr-svr-oclan-01 # awk 'BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/local/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
'sub_report_20130210_10:00:00
output
HTML Code:
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 3
awk: illegal statement near line 3
awk: illegal statement near line 4
awk: syntax error near line 5
awk: bailing out near line 5
root@blr-svr-oclan-01 # 
Sponsored Links
    #6  
Old 02-10-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
I didn't ask you to change usr/xpg4/bin/awk to awk .
Sponsored Links
    #7  
Old 02-10-2013
surender reddy surender reddy is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 4 March 2013, 3:04 AM EST
Posts: 36
Thanks: 10
Thanked 0 Times in 0 Posts
Hi

the script is working now but out put is not exactly as per requirement.

HTML Code:
root@blr-svr-oclan-01 # /usr/xpg4/bin/awk 'BEGIN {print "IP HOST_NAME SUB "}
root@blr-svr-oclan-01 > /IP IS/ {IP=$3}
root@blr-svr-oclan-01 > /local/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
root@blr-svr-oclan-01 > /PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
root@blr-svr-oclan-01 > ' sub_report_20130210_10:00:00
output is coming as below
HTML Code:
IP HOST_NAME SUB 
10.238.48.1 [local]bgl-ras-bng-bge-01 11165
10.238.48.33 [local]bgl-ras-bng-bge-02 8029
10.238.48.65 [local]bgl-ras-bng-bge-03 10364
10.238.48.97 [local]bgl-ras-bng-bge-04 11800
Required output as like below

HTML Code:
IP HOST_NAME SUB 
10.238.48.1 bgl-ras-bng-bge-01 12853
10.238.48.33 bgl-ras-bng-bge-02 9527
10.238.48.193 bgl-ras-bng-jnr-03 13475
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script to mail monitoring output if required or redirect output to log file aix_admin_007 Shell Programming and Scripting 4 10-22-2012 08:49 PM
Script to add text before the first word on a line in a textfile. mjanssen UNIX for Dummies Questions & Answers 3 03-31-2011 03:53 AM
Regarding overwriting the textfile with output what we get! smarty86 Shell Programming and Scripting 6 11-03-2009 08:09 AM
how to get the correct alignment in a textfile to a mail using shell script? suman_dba1 Shell Programming and Scripting 1 04-06-2009 04:11 AM
Script required to get a required info from file. Pls. help me. ntgobinath Shell Programming and Scripting 2 05-31-2008 08:34 AM



All times are GMT -4. The time now is 03:54 PM.