Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello everyone! Q1# I am searching two strings in a log file. Condition is, first it finds string1, then in same line if it finds second string2, it should print the the count of total number of lines.
Since I want to store it in some variable (which must contain a numeric value for further calculation), but the line below count (marked in red color)causing problem. So how can I avoid this extra line in output?
Q2# After printing the lines containing both strings, I want to replace the word contained in $11.
Sample example.log file:
And I want only numeric values i.e. 1234 (by replacing keyword string2 by 'empty'). So how can I combine both 1st (search and print strings) and 2nd (replace strnig) operations using awk?
Last edited by shivaa; 11-30-2012 at 06:43 AM.
Reason: Question fonts size changed
First of all: Can you please stop using the larger font, it doesn't make reading easier or makes your point clearer at all. It also makes it harder to quote, having to remove all the (size) codes. I'm not sure what others think but I find it a bit annoying to be honest. (Fixed by OP)
About your questions:
Quote:
Originally Posted by shivaa
Hello everyone! Q1# I am searching two strings in a log file. Condition is, first it finds string1, then in same line if it finds second string2, it should print the the count of total number of lines.
Since I want to store it in some variable (which must contain a numeric value for further calculation), but the line below count (marked in red color)causing problem. So how can I avoid this extra line in output?
It shouldn't print the red line. The output created by awk is piped to wc and the amount of lines (-l checks for newlines) found should be printed (one number).
I did some testing with the example.log given in question 2 and I cannot get it to print a second line, just the count (5 in this case) is printed.
Are you sure the input file is ASCII text and not some other format? (file example.logs to check).
About your second question:
Quote:
Q2# After printing the lines containing both strings, I want to replace the word contained in $11.
Sample example.log file:
And I want only numeric values i.e. 1234 (by replacing keyword string2 by 'empty'). So how can I combine both 1st (search and print strings) and 2nd (replace strnig) operations using awk?
As for the second one, I can't quite understand your description. But I assume that druuna is right when he guessing you just want to remove the "string2=" part from field 11? He's already demonstrated that the gsub function (or in this case, just sub) can be used to remove such substrings.
Finally, am I right that you want to combine these into a single command? This could be problematic, as the output would necessarily contain both the revised text and the number count. Please clarify exactly what you want to do here.
Hello,
@Druuna: In this file example.log every matching line has simple alphabetic strings in all field, except field 11th. String in $11 has some value against it like:
s0 string1 s1 s2 s3 s4 s5 s6 s7 s8 srting2=1234 s9
So awk will first match both strings, and then will print only string2 i.e. $11 (string2=1234...). Upto this it's working fine. Then in this string2 in 11th field, I want only numeric values (i.e. 1234 etc.). So I want to substitute this "value=" keyword with some empty string, and keep only numeric part.
Following didn't help. Even use of sub function couldn't help.
Its been a while since I worked on a SunOS/Solaris box, but I do remember that more than one awk flavour was available at the time.
nawk (/usr/xpg4/bin/awk if I'm not mistaken) does permit sub and gsub, awk (/usr/bin/awk) does not. It could be that nowadays gawk is also available out of the box, don't know about that though.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.