LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-02-2006, 06:31 AM   #1
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Rep: Reputation: 15
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

Last edited by mike9287; 06-02-2006 at 06:33 AM.
 
Old 06-02-2006, 07:12 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
From your other thread:
Quote:
Originally Posted by you
I shouldnt have been so lazy and posted the way I did
. Now go and post the actual script barring us from delivering the same speech again about us not doing your homework.


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
 
Old 06-02-2006, 07:35 AM   #3
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by unSpawn
From your other thread: . Now go and post the actual script barring us from delivering the same speech again about us not doing your homework.
I am at work and short on time to complete my work as a whole hence me using the internet and utilising a much time as possible. The script I have works fine and I will post it later. I am NOT asking you to do my homework, I simply wanted to know if I could improve the version I have done which I explained it enough detail to get an idea that I was indeed on the right path, the grab info you listed was not exactly what I was looking for as I need to get the info from a file where I redirected the info and not just as a real time variable, but thanks for the input I will post my original script later when I get home.

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

Last edited by mike9287; 06-02-2006 at 07:37 AM.
 
Old 06-02-2006, 08:04 AM   #4
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by unSpawn
Search around for the ABS or Advanced Bash Scripting guide. You'll benefit from reading it.

Hi, found this on Google, looks pretty good, will read through it.

http://www.tldp.org/LDP/abs/html/

Thanks
 
Old 06-02-2006, 04:57 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 06-02-2006, 05:42 PM   #6
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
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
 
Old 06-02-2006, 06:03 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Since you're supposed to use temporary files :}

Code:
#!/bin/bash
for in in 1 2 3 4;
do
env >> myfile
done
awk -F= '$1 == "MAIL" || $1 == "PWD" ||$1 == "HOME" ||$1 == "USER" {line++; print line") " $0}' myfile > myfile.tmp
mv myfile.tmp myfile
 
Old 06-05-2006, 02:12 PM   #8
mike9287
Member
 
Registered: May 2006
Location: North England
Distribution: Mandriva
Posts: 64

Original Poster
Rep: Reputation: 15
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.

Last edited by mike9287; 06-05-2006 at 02:22 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 01:22 PM
help w/ grep john8msu Linux - General 3 04-05-2004 01:44 AM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 02:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 06:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration