1> F"[,.?\! ]" why we used it? and what all these sign (,.?\!) means here can we add more?
-F tells awk what to use as a separator -- where fields stop and start. It's a regular expression. Any single , . ? \ ! will stop the current record and start the next.
Quote:
2> what are the two for loops used for and how they work?
The first for-loop:
loops through i=1, i=2, i=3, ..., i=NF, incrementing arr[$i] each time.
Note the red $, it's important: if i is 1, $i becomes field 1.
So if you had fields A B C D E, it'd do arr["A"]++, then arr["B"]++, then arr["C"]++, and so forth.
NF is a special awk variable for "number of fields". Fields go from 1 to NF inclusive. You can use $NF to mean "the contents of the last field".
Another useful special variable is NR, the 'number of records'. Usually this'd be the line number, but awk can be used to operate on larger blocks than lines, so they consider each thing a 'record' instead.
Quote:
3> what is NF and END?
Already explained NF.
I'll explain about awk codeblocks in general.
The usual {} global code block gets run once for every line, with updated values of NF, NR, $1,$2,... and so forth each time.
You're allowed to have more than one of them.
will print 'asdf' twice.
You can put statements before the block which control when they are run, and statements after the blocks which control when they print. The entire line is printed when you print that way. 'echo asdf | awk '{} 1' will print 'asdf' since the expression '1' is considered always true. You could put 'NR=5' in it instead so it'd only print the fifth line.
END {} tells a code block to run once, after all input has been processed. Useful for printing out things that've been stored but not printed earlier.
BEGIN {} tells a code block to run before any input has been processed. Useful for setting up variables.
/regex/ {} tells a code block to only run when the current line matches the given regular expression.
Hi,
I have a directory with a couple of thousand logs in it. The log files are created every 5 minutes. I want to search these logs grep for a specific sting and more importantly print the name of the files where that sting was found.
e.g.
all logs begin om20020927
what I have been... (4 Replies)
Hi,
I'm rather new to Unix and I'm trying to write a simple script to search through a dictionary for words based on the letters a user would pass as arguments to the script.
Now I have the searching part done. However, the one thig that still eludes me is that I want to only keep the... (4 Replies)
Dear friends,
I have one more query,
incase a file contains multiple tags of same name, then how to get the required string between the tags, in which the string begins with "O/M"
i.e., file1.txt contains following text(please note that all the following tags are in single line)... (6 Replies)
Hi,
Need to extract a string from one file and search the same in other files.
Ex:
I have file1 of hundred lines with no delimiters not even space.
I have 3 more files.
I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Hi ...
I have a string variable
STR = "This is a test message"
I have a file abc.txt that I am searching for the occurence of the string STR ...
I am using the command in a script
cat abc.txt | grep $STR
It identifies each space as a seperator and prints word by word.
How to... (2 Replies)
I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Hi Guys...
I want to search for each file that contains a particular string.
e.g find . -print | xargs grep -i string_name
Now my issue is the files that I search in are gzipped.
Will I be able to find the string, using the above commands, even if the files are gzipped?
Please... (2 Replies)
hello, i'm a novice on bsh scripting so thanks for any help here
basically i have a shell var $x that looks like this
> echo $x
nabc1234:!:73394:17155:Gary Mason:/home/garym:/bin/ksh
and i'm trying to keep the first 8 characters and the text from the 4th : to the 5th :
i've been trying... (9 Replies)
Hi all,
I have some data in the form of
adc|nvhs|nahssn|njadnk|nkfds
in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like
adc|nvhs|nahssn||||njadnk|nkfds
Thanks,
Firestar. (6 Replies)
Hi Guys,
So im trying to search for the most recent occurance of a string in the most recently updated .log file in a certain directory. The string i am searching for is a value, now if this value is greater than 800 i would like an email sent out with some text (blah blah blah).
This is what... (7 Replies)