LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-15-2007, 06:03 PM   #1
whited
Member
 
Registered: Feb 2005
Distribution: Slackware 11
Posts: 109

Rep: Reputation: 15
Need help with bash scripting


So currently I am trying to figure out how to input a list a varibles that are created through a loop and an awk command into a temp document for later sorting.

But I am sure that I will come up with more questions so if anyone is feeling really friendly and aim me at whitedsepdivine I'll tell you my current problem Im working on.

thank you
 
Old 10-15-2007, 06:40 PM   #2
duenez
LQ Newbie
 
Registered: Apr 2006
Location: Cambridge, MA
Distribution: SuSE 11.2
Posts: 5

Rep: Reputation: 0
Would something like this work?

( for i in $*; do awk 'BEGIN{ print '$i'; }'; done ) > temp
 
Old 10-15-2007, 07:47 PM   #3
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Nope, it won't work.

The primes within the AWK program are illegal, you would have to use quotes instead. Unfortunately the $i variable also belongs to Bash.

Here is a fix of the code that should be put in a file and run from the command line as described at the end of this article.

( for i in $*
do
awk -v abc=$i '
BEGIN{ print abc }
'
done ) > temp


1. The brackets tell bash that the process should be run in a shell of it's own, so $* represents all the arguments on the command line.

2. The -v flag defines and initializes the awk variable with the text that follows; or in this case the expansion of $i. This is so because at this point we are calling the AWK script so are still in a Bourn or Bash shell.

3. The primes delimit the bounds of the AWK program proper.

4. BEGIN is an AWK reserved pattern, the action is delimited by the braces.

5. Issues that may be of interest: printing primes or semi-colons in awk require them to be escaped with a backslash or expressed as octal literals.

Code layout on multiple lines is merely my preference and makes it easier, I think, to follow the structure of things. The indentation of the three lines between do and done is lost in the forum. I won't try to fix it as it will be easier to cut and paste as it is.

There are of course many other ways to do the same thing depending on your input requirements.


The code submitted here is in a file called aaa.sh (and chmodded to make it executable) can be called from wherever you write it as:

./aaa.sh now is the time for all good men to come to the aid of the party

If the directory is in your current PATH then it won't need the ./

The content of temp will be:

now
is
the
. . .
of
the
party

One downside is if you wish your data items to contain a space. No amount of quoting the data will allow it. You could of course substitute such spaces with say an underscore and fix it back with another line in the AWK script.

Why not replace:
> temp
with
| sort > temp
adding whatever sort options you wish and temp will contain the list sorted just as you wish.

I hope this has helped. Good luck and good scripting.

PAix

Last edited by PAix; 10-15-2007 at 08:10 PM. Reason: For reader sanity - assume nothing, explain all..
 
Old 10-15-2007, 08:10 PM   #4
whited
Member
 
Registered: Feb 2005
Distribution: Slackware 11
Posts: 109

Original Poster
Rep: Reputation: 15
How do you count the number of unique real words?
 
Old 10-15-2007, 08:16 PM   #5
whited
Member
 
Registered: Feb 2005
Distribution: Slackware 11
Posts: 109

Original Poster
Rep: Reputation: 15
(for file in $( ls | awk '/search[0-9]*_*[0-9]*/' ); do
result=$(awk '/^- Searchfield/{print $3}' $file)
echo $result
done) > temp

Acutally this worked
 
Old 10-15-2007, 08:34 PM   #6
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
On the basis that the file temp is sorted
Quote:
cat temp | uniq | wc -1
otherwise
Quote:
sort temp | uniq | wc -l
 
Old 10-15-2007, 09:54 PM   #7
whited
Member
 
Registered: Feb 2005
Distribution: Slackware 11
Posts: 109

Original Poster
Rep: Reputation: 15
thx man much respect
 
  


Reply

Tags
awk, bash, loop


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
bash scripting drum2jc Linux - Software 7 07-24-2006 01:47 AM
new to bash scripting peok Programming 2 07-15-2006 03:46 AM
Bash scripting disruptive Programming 5 06-29-2006 04:49 PM
help bash scripting sh4d0w13 Linux - Newbie 5 08-15-2005 03:02 AM
bash scripting fnoyan Programming 1 01-18-2005 08:35 AM


All times are GMT -5. The time now is 01:08 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration