Awk Introduction Tutorial – 7 Awk Print Examples

by Sasikala on January 6, 2010

Linux Awk Tutorial - Introduction and Awk Examples

This is the first article on the new awk tutorial series. We’ll be posting several articles on awk in the upcoming weeks that will cover all features of awk with practical examples.

In this article, let us review the fundamental awk working methodology along with 7 practical awk print examples.

Note: Make sure you review our earlier Sed Tutorial Series.

Awk Introduction and Printing Operations

Awk is a programming language which allows easy manipulation of structured data and the generation of formatted reports. Awk stands for the names of its authors “Aho, Weinberger, and Kernighan”

The Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform associated actions.

Some of the key features of Awk are:

  • Awk views a text file as records and fields.
  • Like common programming language, Awk has variables, conditionals and loops
  • Awk has arithmetic and string operators.
  • Awk can generate formatted reports

Awk reads from a file or from its standard input, and outputs to its standard output. Awk does not get along with non-text files.

Syntax:

awk '/search pattern1/ {Actions}
     /search pattern2/ {Actions}' file

In the above awk syntax:

  • search pattern is a regular expression.
  • Actions – statement(s) to be performed.
  • several patterns and actions are possible in Awk.
  • file – Input file.
  • Single quotes around program is to avoid shell not to interpret any of its special characters.

Awk Working Methodology

  1. Awk reads the input files one line at a time.
  2. For each line, it matches with given pattern in the given order, if matches performs the corresponding action.
  3. If no pattern matches, no action will be performed.
  4. In the above syntax, either search pattern or action are optional, But not both.
  5. If the search pattern is not given, then Awk performs the given actions for each line of the input.
  6. If the action is not given, print all that lines that matches with the given patterns which is the default action.
  7. Empty braces with out any action does nothing. It wont perform default printing operation.
  8. Each statement in Actions should be delimited by semicolon.

Let us create employee.txt file which has the following content, which will be used in the
examples mentioned below.

$cat employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

Awk Example 1. Default behavior of Awk

By default Awk prints every line from the file.

$ awk '{print;}' employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

In the above example pattern is not given. So the actions are applicable to all the lines.
Action print with out any argument prints the whole line by default. So it prints all the
lines of the file with out fail. Actions has to be enclosed with in the braces.

Awk Example 2. Print the lines which matches with the pattern.

$ awk '/Thomas/
> /Nisha/' employee.txt
100  Thomas  Manager    Sales       $5,000
400  Nisha   Manager    Marketing   $9,500

In the above example it prints all the line which matches with the ‘Thomas’ or ‘Nisha’. It has two patterns. Awk accepts any number of patterns, but each set (patterns and its corresponding actions) has to be separated by newline.

Awk Example 3. Print only specific field.

Awk has number of built in variables. For each record i.e line, it splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4. $0 represents whole line. NF is a built in variable which represents total number of fields in a record.

$ awk '{print $2,$5;}' employee.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000

$ awk '{print $2,$NF;}' employee.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000

In the above example $2 and $5 represents Name and Salary respectively. We can get the Salary using  $NF also, where $NF represents last field. In the print statement ‘,’ is a concatenator.

Awk Example 4. Initialization and Final Action

Awk has two important patterns which are specified by the keyword called BEGIN and END.

Syntax: 

BEGIN { Actions}
{ACTION} # Action for everyline in a file
END { Actions }

# is for comments in Awk

Actions specified in the BEGIN section will be executed before starts reading the lines from the input.
END actions will be performed after completing the reading and processing the lines from the input.

$ awk 'BEGIN {print "Name\tDesignation\tDepartment\tSalary";}
> {print $2,"\t",$3,"\t",$4,"\t",$NF;}
> END{print "Report Generated\n--------------";
> }' employee.txt
Name	Designation	Department	Salary
Thomas 	 Manager 	 Sales 	         $5,000
Jason 	 Developer 	 Technology 	 $5,500
Sanjay 	 Sysadmin 	 Technology 	 $7,000
Nisha 	 Manager 	 Marketing 	 $9,500
Randy 	 DBA 	 	 Technology 	 $6,000
Report Generated
--------------

In the above example, it prints headline and last file for the reports.

Awk Example 5. Find the employees who has employee id greater than 200

$ awk '$1 >200' employee.txt
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

In the above example, first field ($1) is employee id. So if $1 is greater than 200, then just do the default print action to print the whole line.

Awk Example 6. Print the list of employees in Technology department

Now department name is available as a fourth field, so need to check if $4 matches with the string “Technology”, if yes print the line.

$ awk '$4 ~/Technology/' employee.txt
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
500  Randy   DBA        Technology  $6,000

Operator ~ is for comparing with the regular expressions. If it matches the default action i.e print whole line will be  performed.

Awk Example 7. Print number of employees in Technology department

The below example, checks if the department is Technology, if it is yes, in the Action, just increment the count variable, which was initialized with zero in the BEGIN section.

$ awk 'BEGIN { count=0;}
$4 ~ /Technology/ { count++; }
END { print "Number of employees in Technology Dept =",count;}' employee.txt
Number of employees in Tehcnology Dept = 3

Then at the end of the process, just print the value of count which gives you the number of employees in Technology department.

Recommended Reading

Sed and Awk 101 Hacks, by Ramesh Natarajan. I spend several hours a day on UNIX / Linux environment dealing with text files (data, config, and log files). I use Sed and Awk for all my my text manipulation work. Based on my Sed and Awk experience, I’ve written Sed and Awk 101 Hacks eBook that contains 101 practical examples on various advanced features of Sed and Awk that will enhance your UNIX / Linux life. Even if you’ve been using Sed and Awk for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Sed and Awk utilities.

Additional Awk Articles


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 47 comments… read them below or add one }

1 Steve Mills January 6, 2010 at 3:38 am

I have only just started reading these articles. So far I think they are well written and the explanations are clearly done with an awareness as to how they might possibly be misunderstood and hence extra layers of detail are presented where that might happen. For example, pointing out that the tilde (~) is used to compare with regular expressions when the reader might have otherwise expected an equals sign – without the explanation the reader might have decided that the tilde represented the same thing as an equals sign.

I shall be reading more.

Thanks for posting these articles.

Kind Regards
Steve

2 Daniel Reimann January 6, 2010 at 6:02 am

Thank you for the post here on awk. I use it frequently, but it is always good to have some updates and reminders. Happy New Year.

3 Lawrence January 7, 2010 at 4:34 am

awk is awesome! thanks for your sharing.

Best Regards,
Lawrence

4 Knusper January 9, 2010 at 5:15 pm

Hi… Good article – now I know what ark is, and what I could use it for – well written…. I follow you now on twitter!

5 Harsh January 10, 2010 at 10:08 pm

Thanks for posting a tutorial on awk with illustrated examples.
I Will be expecting other articles on awk :)

6 Ramesh Natarajan January 14, 2010 at 9:23 pm

@Steve,

Yeah. ~ can be little confusing in this context, if not explained properly. Thanks for you comment.

@Daniel,

Yeah. Most other readers of the blog are in similar position like you. So, we are here to give constant updated and remainders of the functionality that they already know.

@Lawrence, Harsh,

Thanks for the very nice comment. I’m glad you liked this article.

@Knusper,

Thanks for following us on twitter.

7 thalafan March 21, 2010 at 10:24 am

Nandraka Ulladhu!!!

I guess the example 2 can be done without a new line like below? Pattern as regex.

~/bin$ awk ‘/Jason|Randy/’ employee
200 Jason Developer Technology $5,500
500 Randy DBA Technology $6,000

And also what does the ; stands for? End of Pattern?

8 Andreia Amaral April 7, 2010 at 5:14 am

Hi,

I want to use an if else statement like this:
if $10>10 print $0 > filename1
else print $0> filename2

but it’s not working it is not creating filename1 or filename2, how can I do this?
thanks?

9 Ashu Agrawal August 6, 2010 at 10:31 am

Grt post.Thanks for making me understand the awk working

10 avinash October 1, 2010 at 7:30 am

hi, this is avinash….
suppose u have a emp table like this:
id name designation salary
1 avi manager 1000
2 ash manager 1500
3 nash manager 2000
4 varma trainee 500
5 chow trainee 600
6 hemanth trainee 800

using awk command, i hav to print manager total salary and trainee total salary….
i need a program….. can any one plz post it

11 vikas October 13, 2010 at 1:34 am

Hi…..@Avinash…..
u can try this one…….
awk ‘BEGIN {man_sal=0;trainee_sal=0;}
$3 ~/manager/ {man_sal+=$NF}
/trainee/ {trainee_sal+=$NF}
END {print “Total salary of manager’s and trainee’s are=”man_sal,trainee_sal}’ in_file.name

12 siva October 15, 2010 at 1:44 am

Hello forum members,

Thanks for AWK tutorials ,it was very help ful to me.

13 avinash October 19, 2010 at 5:12 am

@ vikas
thanks you

14 wish October 21, 2010 at 3:36 am

hi all,
if i have a issue file like:

101 add open vish iuo

if exit and login again i should get the increment of the first field like

102 add open vish iuo

15 mounika October 27, 2010 at 10:40 pm

its simply superb to understand

its is very useful for the beginning learners and its is very help in exams time also

so guys read and enjoy wd the unix

16 Lynda Zhang November 17, 2010 at 12:36 pm

This is very help. How about if I want to print the salary seperated by commas, e.g. 2,000 instead of 2000

17 Ikem December 30, 2010 at 8:22 pm

You’ve made a little typo:

> Number of employees in _Tehcnology_ Dept = 3

18 sudha February 2, 2011 at 12:38 pm

vary vary healpul to every one

19 eben March 27, 2011 at 11:28 pm

Its very useful for beginers like me…………

20 kalandar April 7, 2011 at 8:58 am

Hi,
I found this article to be very useful. Anybody who wants to know what an awk is , this will give a fair idea. Looking forward to similar articles on other topics of unix.

Thanks :)

21 Bhagyaraj April 24, 2011 at 1:38 am

Hi,

Good,
Please try teach in Youtube to try differntly.
It will be more success.

Keep it up,
I need to take an exam on awk, let me see how much I can successed.

22 kernelkid June 10, 2011 at 6:48 am

very simple and easy to understand, thanks a lot, it help me a lot

23 liju June 14, 2011 at 3:05 am

good simple article :)

24 Marija June 30, 2011 at 9:03 am

I have read few geekstuff articles until now, explanations provided are the best I have ever seen so far! Great job :) Thanks a lot :)

25 Muslim July 19, 2011 at 12:02 pm

hi,

i have the question that how to using print command “awk” to sort or transpose this data from many coloums to 2 columns only
#input file
NAME JAN FEB MARCH APRIL MAY JUNE JULY
——- —– —— ———- ——– —— ——- ——
BEN 5,000 6,000 7,000 8,000 6,500 7,500 9,000
YONG 4,000 5,500 6,000 5,800 7,000 8,000 8,5000

# output should be as below.
BEN 5,000
BEN 6,000
BEN 7,000
BEN 8,000
BEN 6,500
BEN 7,500
BEN 9,000
YONG 4,000
YONG 5,500
YONG 6,000
YONG 5,800
YONG 7,000
YONG 8,000
YONG 8,5000
Anyone can help.thanks
@muss

26 nails carmody August 9, 2011 at 1:08 pm

I know it’s late, but …

#!/bin/bash

awk ‘ {
# skip the first two lines
if (NR == 1 || NR == 2)
continue

for(i=2; i<=NF; i++)
printf("%s %s\n", $1, $i)
} ' datafile.txt

Nice site! I learned some new things about sed I didn't know.

27 Sudhanshu August 29, 2011 at 1:00 am

The article is simply awesome!!!

28 Kalim January 25, 2012 at 9:03 am

Very gud information for beginners. Thanks

29 Len Richards February 15, 2012 at 3:46 pm

We have duplicate titles in our library database. I am able to find the duplicates but can’t figure out how to print the line above that matches the regular expression and that line also has the same expression. Each set of duplicates has a space between: example. I would match on SERSOL-EBK

2012 Joe ate my dog|SERSOL-EBK|122456
2012 Joe ate my dog|SERSOL-EBK|122459

2011 Joe ate my toaster|SERSOL-EBK|122433
2011 Joe ate my toaster|SERSOL-EBK|125567

30 Neha March 1, 2012 at 12:37 am

hi..
i want to capture these data through awk.

596583.46875(E) 4924298.34375(N)
geology@PERMANENT in PERMANENT (9)sand

604960.78125(E) 4922837.53125(N)
geology@PERMANENT in PERMANENT (6)shale

596911.40625(E) 4920512.15625(N)
geology@PERMANENT in PERMANENT (4)sandstone

the output should be :
insert into mytable values(596583.46875,4924298.34375,geology@PERMANENT,shale);
insert into mytable values(604960.78125,4922837.53125,geology@PERMANENT,shale);

any help would be grateful.

sorry i need to put shale within single codes so that i can insert into my table.insert into mytable values(596583.46875,4924298.34375,geology@PERMANENT,’shale’);

Thank u

31 subhojit777 June 30, 2012 at 12:55 am

Great article. Thanks you :)

32 Mimi July 6, 2012 at 2:20 pm

Thank you for this site – you really saved me a lot of time. This is easy to follow and really helped me understand awk.

33 sudarshan July 17, 2012 at 7:38 am

Hi All,

I have a log file with the contents as below.

09:30:51 [00082] TIMER iatesystems.hub.logging.TimerLog: MPI_MxmRunMatch: 09:30:52 [00082] TIMER iatesystems.hub.logging.TimerLog: MPI_MxmRunMatch:
09:30:53 [00082] TIMER iatesystems.hub.logging.TimerLog: MPI_MxmRunMatch:

This will have entities based on the timestamp shown above (i.e. 09:30:51, 09:30:52, etc).

Now my requirement is, I need to get the number of entries per an hour.
For example if the first record is with timestamp as 09:30:51.
Till 10:30:51 timestamp’s record I need to get the number of records.

Can you please help me to get this?

Thanks,
Sudarshan

34 Steven August 13, 2012 at 8:54 am

Neha try this
suppose your data filename=log.txt
insert statements be put in insert.sql
————————————————————-
awk ‘BEGIN {str=”insert into mytable values(“;}
{if ((NR % 3)==1)
{ #print “NR1 is:” NR;

instr=str”"substr($1,1,12)”,”substr($2,1,13);
#print “str1 is:” instr;
}
else if ((NR % 3)==2)
{#print “NR2 is:”NR;
instr=instr”,”$1″,”"‘”‘”‘”"ashale”"‘”‘”‘”")”;
print instr >”insert.sql”;
}
else ((NR % 3)==0)
{# print “NR3 is:”NR;
str=”insert into mytable values(“;
}
fi}
END{}’ log.txt

35 Prabhu September 11, 2012 at 5:29 am

Comma delimited file in below format

Name, Address,Start_Date,End_date

But for few records there is extra comma in the Address field
As
Mark, 110A , Stuart Mark, 01/01/2012,06/07/2012

Please help me as how to remove this extra comma, Out of 1 million record am having this issue in 10000 records

36 nails September 11, 2012 at 1:36 pm

Prabhu:
If the number of fields is greater than 4, assume there is comma that makes field 2 into field 2 and field 3. Then, combine field 2 into field 2 and field 3:

awk ‘ BEGIN { FS=OFS=”,” }
{
if(NF > 4)
$2=”$2 $3″
print $0
} ‘ datafile.txt

37 Prabhu September 18, 2012 at 3:38 am

Thanks Nalis, Appreciate your help

38 Kiran Indrala November 7, 2012 at 11:33 am

It is really very helpfull for beginers.i really appreciate you for sharing this info.

Thanks,
Kiran Indrala

39 R.S.Wadhwa December 5, 2012 at 4:46 am

Hello,
I have the following output:
[Server, Modul, Version, timestamp]
Serversv10 ;admin ;1.0.2 ;2012.12.03-14:07:39
Serversv10 ;admin ;0.1.2 ;2012.12.03-14:07:39
Serversv10 ;admin ;0.0.1 ;2012.12.04-09:29:45
Serversv10 ;admin ;0.0.12 ;2012.12.03-10:23:31
Serversv10 ;admin ;0.0.2 ;2012.12.03-14:07:39
Serversv10 ;admin ;1.0.1 ;2012.12.04-09:29:45
Serversv10 ;admin ;0.0.8 ;2012.12.04-09:34:39
Serversv10 ;project ;0.0.1 ;2012.12.03-14:08:37
Serversv11 ;admin ;0.0.6 ;2012.12.03-10:23:31
Serversv10 ;project ;0.0.1 ;2012.12.04-08:22:09
Serversv11 ;admin ;0.0.6 ;2012.12.03-13:58:58
Serversv11 ;admin ;0.0.6 ;2012.12.03-11:39:15
Serversv11 ;admin ;0.0.7 ;2012.12.03-14:07:39
Serversv11 ;basis ;0.0.1 ;2012.12.03-11:39:59
Serversv11 ;project ;0.0.1 ;2012.12.03-12:08:48
Serversv11 ;project ;0.0.1 ;2012.12.03-14:08:37
Serversv12 ;admin ;0.0.6 ;2012.12.03-10:23:31
Serversv12 ;admin ;0.0.6 ;2012.12.03-11:47:35
Serversv11 ;project ;0.0.1 ;2012.12.03-12:08:31
Serversv12 ;admin ;0.0.7 ;2012.12.03-14:07:39
Serversv12 ;basis ;0.0.1 ;2012.12.03-12:06:45
Serversv12 ;config ;0.0.1 ;2012.12.03-12:04:57
Serversv12 ;admin ;0.0.6 ;2012.12.03-13:58:58

and want to sort it as follows
[Server, Modul, Version, timestamp]
Serversv10 ;admin ;1.0.2 ;2012.12.03-14:07:39
Serversv10 ;admin ;0.1.2 ;2012.12.03-14:07:39
Serversv10 ;admin ;0.0.12 ;2012.12.03-10:23:31
Serversv10 ;admin ;0.0.8 ;2012.12.04-09:34:39
Serversv10 ;admin ;0.0.2 ;2012.12.03-14:07:39
Serversv10 ;project ;1.0.1 ;2012.12.03-14:08:37
Serversv10 ;project ;0.0.1 ;2012.12.03-14:08:37
the rest with the same order
Can you please help?

40 nails December 5, 2012 at 11:28 am

R.S.

Sorry, but I do not see a consistent sort order for your output. Please clarify exactly what you want.

Anyway, the Unix/Linux sort command might do what you want. Assuming the field seperator is a semi-colon, this command sorts by the first field, then the second, and then the fourth (the time stamp)

sort -t “;” -k 1,1 -k 2,2 -k 4,4 file.txt

41 Amit December 6, 2012 at 6:23 am

I am having files in below format

2012-04-30 00:00:05,266 1335692570491, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 12486685 / 2012-04-30 00:00:05.196
2012-04-30 00:00:05,313 1335692570492, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 12486685 / 2012-04-30 00:00:05.260
2012-04-30 00:00:12,740 1335692570493, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 83022172 / 2012-04-30 00:00:12.687
2012-04-30 00:00:12,868 1335692570494, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 83022172 / 2012-04-30 00:00:12.822
2012-04-30 23:59:15,450 1335692590664, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 1437954504 / 2012-04-30 23:59:15.404
2012-04-30 23:59:15,645 1335692590665, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 200178220 / 2012-04-30 23:59:15.600
2012-04-30 23:59:17,177 1335692590666, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 1437954504 / 2012-04-30 23:59:17.128
2012-04-30 23:59:18,574 1335692590667, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 200178220 / 2012-04-30 23:59:18.513
2012-04-30 23:59:21,322 1335692590668, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 200178220 / 2012-04-30 23:59:21.274
2012-04-30 23:59:34,467 1335692590669, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 228289416 / 2012-04-30 23:59:34.410
2012-04-30 23:59:34,493 1335692590670, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 228289416 / 2012-04-30 23:59:34.434
2012-04-30 23:59:40,094 1335692590671, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 1195793760 / 2012-04-30 23:59:40.054
2012-04-30 23:59:40,168 1335692590672, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 1195793760 / 2012-04-30 23:59:40.127
2012-04-30 23:59:40,560 1335692590673, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CHECK_REQ / 1195793760 / 2012-04-30 23:59:40.523
2012-04-30 23:59:41,289 1335692590674, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 1195793760 / 2012-04-30 23:59:41.011
2012-04-30 23:59:46,895 1335692590675, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 1174256481 / 2012-04-30 23:59:46.827
2012-04-30 23:59:46,914 1335692590676, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 1174256481 / 2012-04-30 23:59:46.841
2012-04-30 23:59:56,228 1335692590677, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_LIST_REQ / 298656228 / 2012-04-30 23:59:56.167
2012-04-30 23:59:58,399 1335692590678, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_LIST_REQ / 1181525888 / 2012-04-30 23:59:58.318
2012-04-30 23:59:58,499 1335692590679, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 298656228 / 2012-04-30 23:59:58.436
2012-04-30 23:59:58,661 1335692590681, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 298656228 / 2012-04-30 23:59:58.605
2012-04-30 23:59:58,663 1335692590680, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 298656228 / 2012-04-30 23:59:58.600
2012-04-30 23:59:58,706 1335692590682, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 298656228 / 2012-04-30 23:59:58.644
2012-04-30 23:59:58,971 1335692590683, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CHECK_REQ / 298656228 / 2012-04-30 23:59:58.902
2012-04-30 23:59:59,058 1335692590684, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_LIST_REQ / 1220865349 / 2012-04-30 23:59:59.010
2012-04-30 23:59:59,794 1335692590685, Request received from client :: Transaction / PRS_ID / Timestamp :: Amit_CARD_DATA_REQ / 1220865349 / 2012-04-30 23:59:59.741

now i want the output in below format

2012-04-30 | 2012-04-30 23:59:59 | 1335692590685 | Amit_CARD_DATA_REQ | 1220865349

1st field is extracted from the timestamp filed 2012-04-30 23:59:59


In the above the adjacent row will be present as a single row….While copying it in the website its getting split into 2 rows

Please help me on this

thanks,
Amit

42 Badri December 9, 2012 at 2:31 am

Nice and simple article.
example 5 indicates print, angular brackets and ; are optional.. is that correct?

43 Kokanee January 7, 2013 at 12:52 pm

Best AWK introduction I’ve come across. Thanks!!

>K<

44 priya February 17, 2013 at 10:30 am

i want awk program to display employee details

45 Linda March 18, 2013 at 9:37 pm

Hi All,

I have a question on how to read n+ line in a file and substitute the data in a template file.Below is the example

**NAME**

ANNE 80

**Template.txt**

$NAME’s mark is $MARK.

I managed to read the data, substitute into the template and append to RESULT as below.

NAME=`awk ‘{print$1}’ NAME` ; MARK=`awk ‘{print$2}’ NAME`

sed -e “s|NAME|$NAME|” -e “s|MARK|$MARK|” Template.txt >> RESULT

The RESULT will be

ANNE’s mark is 80.

But when the are few rows of data, i dont know how to read the second line and the rest. I have 300 rows of data.

**NAME**

ANNE 80

SHAWN 30

NINA 50

Can anyone help me since im newbiew in scripting.

Thanks in advance :)

46 Eeshani April 18, 2013 at 11:26 pm

Hi all,

I have a doubt
I want to use a variable in the matching of regular expression field
i.e i am reading a string from the user and I want to match it in awk against the whole file

read var;
cat int_out.txt | awk /var instead of reg exp/ { print $1};

Can any1 help me regarding this

thanks

47 Nails Carmody April 19, 2013 at 11:30 am

There are a number of ways of embedding a shell variable in an awk script. This link describes them:

http://tek-tips.com/faqs.cfm?fid=1281

Post a more descriptive example if you require more help. Thanks.

Nails.

Leave a Comment

Previous post:

Next post: