ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
However, it will print all the entires in each column rather than just the first. I'd rather like it to do the above for one line at a time if possible:
Code:
while read line
do
awk -F .....
/usr/bin/systemadd $alias [....]
done
However I can't seem to make any headway. Can anyone help? Does awk have a function whereby it can read a line at a time (however I expect I wont be able to do that as I'll be breaking out of awk to run the systemadd commands..?)?
I don't know of a way to bend awk to do what you're describing, but I'll offer a simple solution: use awk to iterate through the file, and instead of actually running any commands, have awk write output to another file (which will eventually become a shell script).
In other words, the output file will contain lots of entries that look like:
Afterwards, briefly preview the output file, and then run it like a shell script: # sh /output/file
Probably much more elegant ways to accomplish this task -- i.e. with bash or with a more featured language -- but I've used what I just described on many occasions.
Thanks for your input and that makes sense. However, I think the issue I'm hitting is I'm not sure how to print the field in each record (iterate through the file) with awk, rather than just the entire column. Or do you mean perhaps grep each line out, awk it and then form the shell script?
awk can do many things and offer the possibility to run system commands (and interact with their result). However, following your direction I'd suggest to stick with bash, by parsing the file line by line using a while loop.
Code:
while read alias hostname interface ipaddr netmask gateway mac profile
do
/usr/bin/systemadd $alias $hostname $interface $ipaddr $netmask $gateway $mac $profile
done < <(cat file | tr "," " ")
The input to the while loop uses process substitution to pass the output of the tr command as input to the loop. The tr command just translates commas to blank spaces. The read statement can read multiple variables/fields separated by blank spaces (or better by the IFS, Input Field Separator).
If the arguments are passed to the /usr/bin/systemadd command in the same order as the input, you can just use read line and the eval statement to build and execute the command line. Something like (not tested)
Code:
while read line
do
eval /usr/bin/systemadd $line
done < <(cat file | tr "," " ")
@Firebar: You got a good answer from colucix already, but for the sake of posterity I'll give an example of what I was referring to.
Given a file called "data" that contains comma-separated columns value and the output file (which value should be echoed to)...
Code:
$ cat data
Hello,file1
How are you?,file2
Bye,file3
$ awk -F, '{ OFS=" " ; print "/bin/echo " $1 " > " $2 }' data > my-script
$ cat my-script
/bin/echo Hello > file1
/bin/echo How are you? > file2
/bin/echo Bye > file3
$ sh my-script
$ grep '.' file*
file1:Hello
file2:How are you?
file3:Bye
Rather crude, but I am sure you get the idea. awk writes the otherwise monotonous script for you. (To be fair, three rows of data is not monotonous; but three-thousand rows of data could be.)
The input to the while loop uses process substitution to pass the output of the tr command as input to the loop. The tr command just translates commas to blank spaces. The read statement can read multiple variables/fields separated by blank spaces (or better by the IFS, Input Field Separator).
Wow, a very elegant way of doing it without awk. I was too busy munging some bash with awk
Quote:
Given a file called "data" that contains comma-separated columns value and the output file (which value should be echoed to)...
Thank you, that explains it clearly to me.
Thanks for the input all. I'm going to go away and give all the methods a go to try and explore some more.
As stated you could easily do all in awk, but depending on how many operations you wish to perform for each line I would follow colucix's suggestion to just keep it in bash.
As such, here is another alternative:
Code:
#!/bin/bash
while read -r
do
set - ${REPLY//,/ }
#each item from the file is now a positional parameter
#ie $1 equals alias based on your post
/usr/bin/systemadd $1 $2 ...
done<file
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.