File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Linux / UNIX and the fly likes split  function problem  in AWK Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "split  function problem  in AWK" Watch "split  function problem  in AWK" New topic
Author

split function problem in AWK

satya kiran
Ranch Hand

Joined: Nov 07, 2000
Posts: 52
while trying to split and display the given name using awk split function, i am experiencing problems.
What I want is:
File:a.sh
awk '{split("java-ranch",a,"-")}'
print ${a[1]}
should print java.
The Problems I'm Experiencing:
awk '{split("java-ranch",a,"-")}' seems to be hanging (never returned to the prompt unless i hit ctrl-c)
If i give
awk split("java-ranch",a,"-") i am getting "Unexpected symbol '(' encountered " error message.

Please let me know where i am going wrong?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59



The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

Hi Satya,
You do have to give awk an input to work with, which is why I have an echo statement piping into the awk statement. I dont do anything with the input, but it makes your statement work.
Without the echo statement, awk will wait for input from the keyboard. This appears to hang, so most people do as you did - press Ctrl-C. However you should have been able to just hit enter, and the split command would have worked.
The next problem is that the print statement was not in the awk statement.
Rather than disecting your code any further, perhaps you can look at my code and if you have questions, ask them.
Regards, Andrew
satya kiran
Ranch Hand

Joined: Nov 07, 2000
Posts: 52
Thanks Andrew!
Thats great..and it worked. I didnt find any person in my office who knows the answer.

Thanks alot.
satya
satya kiran
Ranch Hand

Joined: Nov 07, 2000
Posts: 52
Andrew!
One more question. How can i assign the value in the a[1] to a variable?
For example:
I want to connect to a database with user name and password. which i will getting from some other string in the form of "username/password". Inorder to prevent people from looking into the actual values, i want to read the user name and password into some other variables and use them in the program.
If i give echo before awk command it will display the values to the console. How can I prevent that?
File: a.sh
Values = "Username/Password"
awk '{split(values,a,"//")}'
user= a[1]
password=a[2]
connect database using $user and $password
How can i achieve this?? As I am new to shell programming, getting too many questions.
Thanks in advance for your help.
Ramesh
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

Hi Satya / Ramesh
Not sure how you are gettting database connectivity through awk, but never mind....
Since you do not want to use the echo statment, I assume you also do not want to use the "-v" parameter:

Instead of having the username / password on the command line, you can store them in a file and read that file from within awk. I recommend against storing them in the shell script that the user is running, because the user has to have the ability to read the script in order to run it. Therefore they could just "cat script" and see the username and password. Trying to set this up in a secure way could be problematic. I would probably look at using sudo or something like that, so that the user can run the script as another user, where the other user has the abilitiy to read the script, but the normal user doesnt. (There are ways of hiding that from the user as well).
Anyway, some examples, using the standard ".netrc" file to store the passwords (man netrc to find out what this file is for if you dont already know).

However, this means that you cannot pipe anything else into the awk statement. Assuming you want to be able to have the awk statement process input at the same time as getting the username and password:

Without the two comments, this could be a single line awk statement (not very readable though
Regards, Andrew
satya kiran
Ranch Hand

Joined: Nov 07, 2000
Posts: 52
Thank you Andrew!
I did as per your suggestions and it worked. Thanks alot.

Regards
Satya.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: split function problem in AWK
 
Similar Threads
Reading A CSV File
Expression to get Tomcat pid?
What is this error?
Read windows registry REG_DWORD value with java.util.prefs.WindowsPreferences
How to read each line from file in Shell script