How to use awk with while read line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use awk with while read line
# 1  
Old 04-07-2011
How to use awk with while read line

Hi All,

Content of mydatafile-

Code:
Name              Age
--------------     ---------------
Raju P             20 years
Hari                25 years
Priya S            30 years

I need output like-

Code:
The age of Raju P is 20 years
The age of Hari is 25 years
The age of Priya S is 30 years

And I want to use while read & awk in my script. My code should be something like-

Code:
while read line
do
  echo 'The age of $1 is $2'   # Here awk has to be used to give $1 & $2
done < mydatafile

I know my above code is wrong. Please let me know how to use awk in my code.

Thanks,
Naresh

Last edited by Scott; 04-07-2011 at 08:44 PM.. Reason: Absolutely final reminder to use CODE TAGS
# 2  
Old 04-07-2011
Code:
awk '/<code>/{print $0; getline; while ($0~/^Name|^----/){getline;}}NF>2{x=$(NF-1) FS $NF;NF=NF-2;$1=$1; print "The age of " $0 " is " x}/<\/code/{print$0}' infile

---------- Post updated at 09:22 PM ---------- Previous update was at 09:19 PM ----------

Code:
# cat tst
<code>
Name Age
-------------- ---------------
Raju P 20 years
Hari 25 years
Priya S 30 years
</code>
<code>
Name Age
-------------- ---------------
Raju P 20 years
Hari 25 years
Priya S 30 years
</code>

Code:
# awk '/<code>/{print $0; getline; while ($0~/^Name|^----/){getline;}}NF>2{x=$(NF-1) FS $NF;NF=NF-2;$1=$1; print "The age of " $0 " is " x}/<\/code/{print$0}' tst
<code>
The age of Raju P is 20 years
The age of Hari is 25 years
The age of Priya S is 30 years
</code>
<code>
The age of Raju P is 20 years
The age of Hari is 25 years
The age of Priya S is 30 years
</code>

# 3  
Old 04-07-2011
Code:
#!/bin/bash
while read line
do
 var=($line)
 length=${#var[@]}
 if [ "${var[$length-1]}" = "years" ]
 then
  name=${line/ ${var[$length-2]} ${var[$length-1]}}
  echo "The age of "$name" is "${var[$length-2]} ${var[$length-1]}
 else 
  echo $line
 fi
done
 
./script file

<code>
Name Age
-------------- ---------------
The age of Raju P is 20 years
The age of Hari is 25 years
The age of Priya S is 30 years
</code>

# 4  
Old 04-07-2011
First of, you should use [code] and [/code] not <code> and </code>

Code:
Name                       Age
-------------- ---------------
Raju P                20 years
Hari                  25 years
Priya S               30 years

Code:
$ awk '/years$/ {$(NF-1)="is "$(NF-1); $1="The age of "$1; print $0}' mydatafile
The age of Raju P is 20 years
The age of Hari is 25 years
The age of Priya S is 30 years

If you really want to use read and while (cant think why):
Code:
while read
do
    echo "$REPLY" | awk '/years$/ {$(NF-1)="is "$(NF-1); $1="The age of "$1; print $0}'
done < mydatafile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

2. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

3. Shell Programming and Scripting

Read input file with in awk script not through command line

Hi All, Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab. Cat my.awk #!/bin/awk -f function test(var){ some code} { } END { print"test code" } (5 Replies)
Discussion started by: random_thoughts
5 Replies

4. Shell Programming and Scripting

Read column values from previous and next line using awk

Hi, I have a csv file which contains data that looks something like this: Key1 Key2 Key3 New_Key1 New_Key2 New_Key3 102 30 0 - - - 102 40 1 30 40 50 102 50 2 40 50 ... (4 Replies)
Discussion started by: Nishi_Licious
4 Replies

5. Shell Programming and Scripting

IF awk in a while read line-loop

Hi As a newbe in scripting, i struggle hard with my first script. What i want to do is, bringing data of two files together. file1: .... 05/14/12-04:00:00 41253 4259 5135 5604 5812 5372 05/14/12-04:10:00 53408 5501 6592 7402 7354 6639 05/14/12-04:20:00 58748 6037 7292 8223... (13 Replies)
Discussion started by: IMPe
13 Replies

6. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

7. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

8. Shell Programming and Scripting

awk line to read file

OK, I want to read a textfile `base.txt` looking like this: MASTER SLAVE PERP_BASE /_\ DAYS /_\ DOPPLER x20080613 x20080829 123.3909 77 11.04877 x20080613 x20080909 -5.9504 88 -12.78454 x20080613... (2 Replies)
Discussion started by: friend
2 Replies

9. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

10. Shell Programming and Scripting

Read logline line by line with awk/sed

Hello, I have a logfile which is in this format: 1211667249500#3265 1211667266687#2875 1211667270781#1828 Is there a way to read the logfile line by line every time I execute the code and put the two numbers in the line in two separate variables? Something like: 1211667249500#3265... (7 Replies)
Discussion started by: dejavu88
7 Replies
Login or Register to Ask a Question