The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert line break in vi's command mode Skogsmulle UNIX for Dummies Questions & Answers 3 07-06-2007 11:47 AM
Replacing characters in file with line break johnemb Shell Programming and Scripting 10 04-26-2007 08:38 AM
Sed Help (Using expression - line break) Janus Shell Programming and Scripting 2 02-16-2007 08:50 PM
Remove Line Break Rock Shell Programming and Scripting 5 02-06-2007 09:54 AM
Trim whitespace and add line break moose1 Shell Programming and Scripting 7 01-22-2007 01:53 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-28-2007
aajan aajan is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 80
TO break a line

hi All,
Have a doubt in ksh..Am not familiar with arrays but i have tried out a script..
plzzzzz correct me with the script

My i/p File is:

(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = 192.168.2.2)
(Port = 1525)
)
)
(CONNECT_DATA = (SID = TESTDB1)
)
)


(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = 192.168.2.4)
(Port = 1525)
)
)
(CONNECT_DATA = (SID = TESTDB2)



And My script is:


while read line
do
i=0

a[$i]=`grep "Host" |cut -d" " -f3 |tr -d ')'`
echo ${a[$i]}
i=`expr $i + 1`
done < File Name




Am trying to get the value of host and assign it to an array


My O/p is


192.168.2.2 192.168.2.4


But i need the o/p to be:

a[0]=192.168.2.2

a[1]=192.168.2.4
  #2 (permalink)  
Old 08-28-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,957
Code:
awk -F" = " '/Host/ { arr[i++] = substr($2, 0, length($2) - 1) }END{ for ( i in arr ) { print i, arr[i] } }' filename
  #3 (permalink)  
Old 08-28-2007
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
change the echo statement to

Code:
echo "${a[$i]} \n"
  #4 (permalink)  
Old 08-28-2007
aajan aajan is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 80
Thank Matrix!!!!!!!!!!

But how to store the value in an array

Like

Host[0]=192.168.2.2
Host[1]=192.168.2.4
  #5 (permalink)  
Old 08-28-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,957
its already in the array named 'arr'

thats what I have printed in the END block of awk.

Are you looking for displaying it in the format posted ?
  #6 (permalink)  
Old 08-28-2007
aajan aajan is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 80
Yes Am looking for the Format which i have given in the o/p
  #7 (permalink)  
Old 08-28-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,957
Quote:
Originally Posted by aajan View Post
Yes Am looking for the Format which i have given in the o/p

Just modify the print clause in the awk command to,

Code:
awk -F" = " '/Host/ { arr[i++] = substr($2, 0, length($2) - 1) }END{ for ( i in arr ) { printf "Host[%d]=%s\n", i, arr[i] } }' filename
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0