Elagance and grep
Hi,
is there are more elegant way to use grep than the following in my script: grep PWD myfile grep USER myfile grep HOME myfile grep MAIL myfile etc Also I have a finished script on my computer at home which extracts certain data from a file which I re-directed "env" to 5 times, I the had to list the above info PWD, USER etc and add a line number with a bracket like so to the left "1)" "2)" etc. The file works fine and is complete but looks really untidy, I used sed to insert the ")" first then an awk command to insert the number: awk '{print NR,$0 }' myfile <------- something like this. Any ideas on how I can improve my design a little? or even just a smarter way to getting the numbers and bracket in place. the output of my scritp is like this 1) PWD /home/michael.kelly 2) USER /michael.kelly etc, which is fine but the script looks like crap. Thanks Mike |
From your other thread:
Quote:
grep PWD myfile If you meant to just grab the current available environment variable $PWD, then just use it: "a=( $PWD $USER $HOME ); echo "${a[0]}"". *use array here = save typing below. Search around for the ABS or Advanced Bash Scripting guide. You'll benefit from reading it. add a line number with a bracket For displaying or interactive use? If using try "select crap in ${a[@]}; do something; done" Elif displaying you could "i=0; for s in ${a[@]}; do ((i++)); echo "$i) $s"; done" but the script looks like crap. If it doesn't randomly wipes out large tracts of land at the press of a getkey but "just gets the job done" nobody should mind I'd say. We prolly have seen worse ;-p |
Quote:
I googled for a while last night and some this morning and couldnt find any info there so posted here, also checked "man" which dosent expand on this function enough..well not for a newbie like me anyways. Thanks again. Mike |
Quote:
Hi, found this on Google, looks pretty good, will read through it. http://www.tldp.org/LDP/abs/html/ Thanks |
Mike,
as for the various greps: You could use egrep. egrep -e"(MAIL|USER|HOME|PWD)" myfile As for the insertion of ) and the line-number: You could have done all that in the awk-script. In fact, you could even tie the grep into the awk ;} (Not that I'm certain that that's what you want). awk -F= '$1 ~ /MAIL/ || $1 ~ /PWD/ ||$1 ~ /HOME/ ||$1 ~ /USER/ {print NR ") " $0}' myfile Cheers, Tink |
Hi,
Nice one, thats exactly what I wanted to do, I had the script do what I required but in an ugly way, like so: grep -m 1 PWD xx > temp grep -m 1 USER xx >> temp grep -m 1 MAIL xx >> temp grep -m 1 HOME=/michael.kelly/xx >> temp cp temp temp2 cat temp temp2 > myfile sed -e 's/.*) &/' myfile > temp cat temp > myfile awk '{print NR, $0 }' myfile > temp2 cat temp2 > myfile rm temp temp2 The question was: create a file as follows env >> xx env >> xx env >> xx env >> xx env >> xx Then create a script that takes one argument being a file, in this instance we will use the the newly created file above xx read the inputted file, in this case xx and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display the line number and a bracket against the saved line. My finished file should look something like this: 1) PWD=/home/michael.kelly 2) USER=michael.kelly 3) MAIL=/var/spool/mail/michael.kelly 4) LOGNAME=michael.kelly 5) HOME=/home/michael.kelly 6) PWD=/home/michael.kelly 7) USER=michael.kelly 8) MAIL=/var/spool/mail/michael.kelly 9) LOGNAME=michael.kelly 10) HOME=/home/michael.kelly which my script did but looked pretty long, I know I didnt have to type all this out but I wanted to prove I wasnt just looking for easy answers, I simply wanted to improve and what I had all ready done to learn more. Your post helped greatly. Thanks Mike |
Since you're supposed to use temporary files :}
Code:
#!/bin/bash |
Hi (again ;) )
I need a little help, I have been told that I am not allowed the use of a temp file so I need to work with the one file which is myfile. Here is my script so far which either gives me a blank file called "myfile" or if I alter it slightly gives me 2 lots of input, one which is not numbered followed by the same data with numbers and the bracket, I can get the output I want on screen but I can't seem to save it. egrep -m 10 "(PWD|USER|MAIL|LOGNAME|HOME)" xx > myfile awk '{print NR") "$0 }' myfile > myfile How do I save the ouput in myfile to look like this: EDIT: Its okay, I got it sorted. I used a pipe after xx on line one and only saved at the very end. Thanks Mike 1) PWD=/home/michael.kelly 2) USER=michael.kelly 3) MAIL=/var/spool/mail/michael.kelly 4) LOGNAME=michael.kelly 5) HOME=/home/michael.kelly 6) PWD=/home/michael.kelly 7) USER=michael.kelly 8) MAIL=/var/spool/mail/michael.kelly 9) LOGNAME=michael.kelly 10) HOME=/home/michael.kelly I know I could use the AWK script that Tinkster wrote but I would prefer to use the above methods as I understand them better, the script Tinkster wrote also is probably a bit advanced, at least for me anyways at this stage. Cheers Mike EDIT: Its okay, I got it sorted. I used a pipe after xx on line one and only saved at the very end. |
All times are GMT -5. The time now is 11:03 PM. |