LinuxQuestions.org
Help answer threads with 0 replies.
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 04-01-2010, 09:53 AM   #1
dragonandante
LQ Newbie
 
Registered: Feb 2010
Posts: 4

Rep: Reputation: 0
Awk Related Question


I'm trying to get the following script to work.

Quote:
while read line
do

awk '
/$line/ {
print $2
print $3
print "\r"
}
' changes.txt > email.txt

# Add in a email line later

done < "usernames.txt"
However this doesn't work. Also, I'm trying to get it to print the very next line as well.

Here are examples of changes.txt and username.txt

Quote:
username.txt

username1
username2
username3
Quote:
changes.txt

username1 somedate /somedirectory
Comment: I enjoy some moist cake
username2 somedate /somedirectory
Comment: Pie is better
username3 somedate /somedirectory
Comment: No love for cookies?
username1 somedate /somedirectory
Comment: Hell no, cake all the way
So each user would get an email detailing only what they did.
For example usernam1 would get an email with the following in the body:

Quote:
username1 somedate /somedirectory
Comment: I enjoy some moist cake

username1 somedate /somedirectory
Comment: Hell no, cake all the way
So to sum it up, I'm trying to get awk to cycle through the usernames list, and to print the very next line as well. Any help or hints will be greatly appreciated.

Last edited by dragonandante; 04-01-2010 at 09:54 AM.
 
Old 04-01-2010, 12:44 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
Code:
while read line; do

   awk '/$line/ {print $2print $3print "\r"}' changes.txt > email.txt
   # Add in a email line later

done < "usernames.txt"
(condensed for space and readability)

Well, first of all there's your variable. A bash variable can't expand when enclosed inside single quotes.

To use a bash variable in awk, you either have to quote it carefully, or import it into an awk variable, as explained here.

http://www.grymoire.com/Unix/Awk.html#uh-4

In addition, you generally only use a single print command per line, and you can't just use a carriage return to get the next line. awk has a special "getline" function for that (not to mention that newlines are represented by "\n" anyway).

So here's my solution.
I couldn't make it import and use an awk variable correctly , but I did manage to get it to work by simply "unquoting" the variable. That is, the awk parts of the expression are in single quotes, and the bash variable is in double quotes.
Code:
while read line; do

   awk '/'"$line/"' {print "User:", $0; getline; print "Comment:", $0"\n"}' changes.txt > email.txt
   cat email.txt

done <usernames.txt
A real awk guru can probably clean it up even more.

Last edited by David the H.; 04-01-2010 at 01:35 PM. Reason: cleanup and correction
 
1 members found this post helpful.
Old 04-01-2010, 01:15 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
Stupid me. I was making things too complicated. Here's a way to do it through importing the bash variable into an awk variable (the -v option).

Code:
while read line; do

   awk -v ln="$line" '{if ($1==ln){print "User:", $0; getline; print "Comment:", $0"\n"}}' changes.txt > email.txt
   cat email.txt

done <usernames.txt
This format has the advantage in that it matches the username exactly.

Last edited by David the H.; 04-01-2010 at 01:34 PM. Reason: made is simpler
 
1 members found this post helpful.
Old 04-01-2010, 04:38 PM   #4
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
Is not enough using grep to get what he wants ?
Code:
while read line; do
    grep --after-context=1 $line changes.txt
done <usernames.txt >email.txt
 
1 members found this post helpful.
Old 04-02-2010, 01:15 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Or you could just do it all in awk:

Code:
#!/usr/bin/awk -f

BEGIN{
    i = 0
    while( (getline line < ARGV[2]) > 0)
        arr[i++] = line
}

FILENAME == ARGV[1]{

    for (x in arr)
        if (arr[x] ~ $0)
            print arr[x]"\n"arr[x+1] >> "email.txt"

    print "" >> "email.txt"
}
 
1 members found this post helpful.
Old 04-05-2010, 07:37 AM   #6
dragonandante
LQ Newbie
 
Registered: Feb 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Excellent, many thanks for the help everyone.
 
  


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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
awk loop related kalyanofb Programming 3 02-16-2007 02:31 AM
Awk question blanny Linux - Software 6 05-02-2006 08:03 AM
awk question denalitastic Linux - Newbie 1 06-07-2005 10:42 AM
c related question. blackzone Programming 1 07-24-2004 08:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:13 PM.

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