Summary using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Summary using awk
# 1  
Old 01-14-2010
Summary using awk

Displaying information using awk
Hey guys, i am using awk to display my information in a certain order.

Database :
Code:
 Persia:42:John
France:50:Mabel 
Persia:50:Rach
Germany:60:John

My expected output is :

Code:
Code:
Title             Price           Author
Persia             42              John
France             50              Mabel
Persia             50              Rach
Germany            60              John

So i first tried using awk command which goes like this :
Code:
Code:
Title=$(awk -F":" '{printf"\n" $1 }' fruit)
       Price=$(awk -F":" '{printf"\n" $2 }' fruit)
       Author=$(awk -F":" '{printf"\n" $3 }' fruit)

Reason being, i was thinking by arranging the variables under the each Columm name such as Title, Price and Author, it will arrange itself neatly. Let me show u what i meant.

Code:
Code:
echo  Title             Price           Author
echo  $Title             $Price          $Author

but for some reason, when i save the results in a variable, the formatting will be gone, instead of displaying the information in a neat columm, it displays the information on one line like the example below :

Code:
Code:
Title             Price           Author
PersiaFrancePersiaGermany

I tried using cut command but it also gives me the same problem. If i do not assign it to a variable, how am i able to format it to display it in the correct format?
# 2  
Old 01-14-2010
try:
Code:
awk 'BEGIN { FS = ":" ;print "Title          Price          Author" } { printf "%-15s%-15s%-15s\n",$1,$2,$3}' file

# 3  
Old 01-14-2010
You could do this all in one awk without declaring multiple awk results to shell variables. When using in printf in shell or awk etc., you can use decimal values etc. to format the output so you have fixed widths between the values.

Here an example:
Code:
awk -F: 'BEGIN{printf("%-20s%-20s%-20s\n", "Title", "Price", "Author")} {printf("%-20s%-20s%-20s\n", $1,$2,$3)}' infile
Title               Price               Author
Persia              42                  John
France              50                  Mabel
Persia              50                  Rach
Germany             60                  John

# 4  
Old 01-14-2010
For shells that know <( )
Code:
awk -F: '$1=$1' OFS="\t\t" <(echo "Title:Price:Author") fruit

-or-
Code:
echo "Title:Price:Author" | cat - fruit | awk -F: '$1=$1' OFS="\t\t"

Code:
Title           Price           Author
Persia          42              John
France          50              Mabel
Persia          50              Rach
Germany         60              John

# 5  
Old 01-14-2010
Thanks for the help guys. This question does not have anything to do with the post but i am just curious. i read up about sed command for delete and for it to delete, you need to specify one input.

for example :
Code:
 sed '/$Title/d' fruit > fruittemp

but the problem with this is that it deletes entire rows which contain this information.

so i tried putting in another command which will narrow down which row needs to be deleted.

Code:
grep $Title fruit | grep $Author | sed '/$Title/d' fruit > fruittemp

but this also deletes any row which contains Persia as the title. How do i go about narrow down its seach to a specific row using two input and then deleting it?
# 6  
Old 01-14-2010
Code:
awk '/'$Title'/&&/'$Author'/{$0="" ; next}1'

SmilieSmilie
# 7  
Old 01-14-2010
Thanks again ahmad. I understand how this code works for the most part, but can i ask , what is the"next" command for in the line and what does the "1" stand for.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Files summary using awk based on index key

Hello , I have several files which are looking similar to : file01.txt keyA001 350 X string001 value001 keyA001 450 X string002 value007 keyA001 454 X string002 value004 keyA001 500 X string003 value005 keyA001 255 X string004 value006 keyA001 388 X string005 value008 keyA001 1278 X... (4 Replies)
Discussion started by: alex2005
4 Replies

2. Shell Programming and Scripting

Generating summary data (use awk?)

I have a data file similar to this (but many millions of lines long). You can assume that it is totally unsorted but has no duplicate rows. Date ,Tool_Type ,Tool_ID ,Time_Used 3/13/2014,Screwdriver,Screwdriver02, 6 3/13/2014,Screwdriver,Screwdriver02,20... (2 Replies)
Discussion started by: Michael Stora
2 Replies

3. Shell Programming and Scripting

Using awk to create a summary of a structured file

I am trying to use awk to create a summary of a structured file. Here is what it looks like: (random text) H1 H2 H3 H4 44 78 99 30 31 -- 32 21 12 33 55 21 I'd like to be able to specify a column, say H2, and then have information about that column printed. ... (4 Replies)
Discussion started by: afulldevnull
4 Replies

4. Shell Programming and Scripting

summary line help

$ cat file a:12:56:12 b:23:12:23 d:32:24:12 c:90:12:24 required output: a:12:56:12 b:23:12:23 d:32:24:12 c:90:12:24 t:157:104:71 t line is the total line, which will be the last line in the output. Please help. I tried this: (4 Replies)
Discussion started by: uwork72
4 Replies

5. UNIX for Dummies Questions & Answers

Directory Summary Command

Is there a unix command to provides the number of files in a directory? (2 Replies)
Discussion started by: rbarlow
2 Replies

6. UNIX for Dummies Questions & Answers

System Summary Tools

Are there any system sumary tools for Linux? What are some good ones for Solaris and HP-Ux? (2 Replies)
Discussion started by: vader
2 Replies
Login or Register to Ask a Question