• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

split function problem in AWK

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Andrew Monkhouse
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Andrew!
I did as per your suggestions and it worked. Thanks alot.

Regards
Satya.
 
reply
    Bookmark Topic Watch Topic
  • New Topic