Script to get required output from textfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to get required output from textfile
# 8  
Old 02-11-2013
What about Post #3? You might consider using /.local.|#cont/ as a search pattern if your awk flavour doesn't like the \[...\] construct.
And, as elixir_sinari pointed out (thank you!) , use gsub in lieu of sub.
# 9  
Old 02-11-2013
Hi

I tried the below code but output is not as desired.

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.|#cont/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
root@blr-svr-oclan-01 > /PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
root@blr-svr-oclan-01 > ' /Scripts/sub_report.txt 
output is coming as below

HTML Code:
IP HOST_NAME SUB 
10.238.52.65 [local]pun-ras-bng-mhs-01 13623
10.238.54.1 [local]enk-ras-bng-cse-01 19610
10.238.56.225 [local]ngp-ras-bng-cto-01 19090
root@blr-svr-oclan-01 # 
# 10  
Old 02-11-2013
Quote:
Originally Posted by surender reddy
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.|#cont/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
root@blr-svr-oclan-01 > /PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
root@blr-svr-oclan-01 > ' /Scripts/sub_report.txt
This should be one line of code. Ok,you can put the awk code in a file and run it like this:
Code:
# cat > script.awk
BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/.local.|#cont/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
#
# awk -f script.awk /path/to/Scripts/sub_report.txt
IP HOST_NAME SUB
10.238.48.225  10064
10.238.49.1 chn-ras-bng-hdw-01 7544
#

# 11  
Old 02-11-2013
Hi

code is modifed,output is not coming

[HTML]

root@blr-svr-oclan-01 # more script.awk
BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/.local.|#cont/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}


HTML Code:
root@blr-svr-oclan-01 # /usr/xpg4/bin/awk -f script.awk /Scripts/sub_report.txt
HTML Code:
IP HOST_NAME SUB 
10.238.52.65 [local]pun-ras-bng-mhs-01 13623
10.238.54.1 [local]enk-ras-bng-cse-01 19610
10.238.56.225 [local]ngp-ras-bng-cto-01 19090
output is like below

if iam using only awk ,then output is like below





HTML Code:
root@blr-svr-oclan-01 # awk -f script.awk /path/to/Scripts/sub_report.txt
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
root@blr-svr-oclan-01 # 

Last edited by surender reddy; 02-11-2013 at 06:29 AM..
# 12  
Old 02-11-2013
Make that line read
Code:
/local/ {gsub (/.local.|#cont/,"");(HOST=$1;}

# 13  
Old 02-11-2013
Hi

modified the code but output is not coming this time
code
HTML Code:
root@blr-svr-oclan-01 # more script.awk 
BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/local/ {gsub (/.local.|#cont/,"");(HOST=$1;}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
HTML Code:
root@blr-svr-oclan-01 # /usr/xpg4/bin/awk -f script.awk /Scripts/sub_report.txt
output
HTML Code:
/usr/xpg4/bin/awk: file "script.awk": line 3: syntax error  Context is:
>>>     P=$3}
>>>     /local/ {gsub (/.local.|#cont/,"");(HOST=$1;    <<<
root@blr-svr-oclan-01 #
# 14  
Old 02-11-2013
Sorry, one parenthesis too many due to sloppy copying... make it
Code:
/local/ {gsub (/.local.|#cont/,"");HOST=$1}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

2. Ubuntu

Top output to textfile not working in cronjob

Hello, If i run the following script from the cmdline it works just fine however if i run it via root cronjob it doesn't write to the file at all. It is chmod 777 now so shouldnt be any right issues. Shellscript looks like this: /usr/bin/top -b -n 1 -u nagios >> /var/log/test.log Cronjob... (7 Replies)
Discussion started by: heady89
7 Replies

3. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

4. UNIX for Dummies Questions & Answers

Script to add text before the first word on a line in a textfile.

How can i make a script to add text before the first word on a line in a textfile : Example: Old line: is my place New line: this is my place Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (3 Replies)
Discussion started by: mjanssen
3 Replies

5. Shell Programming and Scripting

Regarding overwriting the textfile with output what we get!

Hi freinds, My problem is I have a script which is used for our office work... in that with my userid i can append the LOGFILE with the output which i get while running the script.. for this i use the below command output >> $LOGFILE but with the same script my colleague is... (6 Replies)
Discussion started by: smarty86
6 Replies

6. Shell Programming and Scripting

perl required output

Hi, I have a string in log file from that i need to pick the username. the string is like this-- pid 2172 tid 3124: 160: 10110847: userName : pid 2172 tid 3124: 160: 10110847: userName : pid 2172 tid 3124: 160: 10110847: userName : pid 2172 tid 3124: 160: 10110847: userName : pid... (5 Replies)
Discussion started by: namishtiwari
5 Replies

7. Shell Programming and Scripting

output required

Hi I have the following output Message man amm (9196) is calling Hello & Alert man amtrr (9197) is stopped Find amfi (19198) is cancelled Engine Item aea (19203) is notified Engine Item2 aea2 ... (8 Replies)
Discussion started by: PrasannaKS
8 Replies

8. Shell Programming and Scripting

how to get the correct alignment in a textfile to a mail using shell script?

Hi All, Can any one solve this prob. Im sending a textfile content as a mail body while sending a mail using shell script. But, that textfile content is a unic command output. So, in mail, im not getting the correct alignment as in textfile. What may be the reason behind this. Can... (1 Reply)
Discussion started by: suman_dba1
1 Replies

9. Shell Programming and Scripting

help me how to use nawk for required output

Hi all i have 2 input files 1st file is N1 | N2|N3|N4|N5|N6|N7|N8|N9 4041491000|245160|1|0|0|1|0|0|0 4041401505|152178|1|1|1|1|0|0|0 4041450004|014052|1|1|1|1|0|0|0 4041450005|580916|1|1|1|1|0|0|0 4041491000|230990|1|0|1|1|0|0|0 4041460001|338317|1|1|1|1|0|0|0 2nd file N1 |... (8 Replies)
Discussion started by: dodasajan
8 Replies

10. UNIX for Advanced & Expert Users

formatting textfile inside ksh script using awk not working

I cannot seem to get this text file to format. Its as if the awk statement is being treated as a simple cat command. I manned awk and it was very confusing. I viewed previous posts on this board and I got the same results as with the the awk command statement shown here. Please help. ... (6 Replies)
Discussion started by: tekline
6 Replies
Login or Register to Ask a Question