Sponsored Content
Full Discussion: Searching a String
Top Forums Shell Programming and Scripting Searching a String Post 302555791 by Corona688 on Thursday 15th of September 2011 03:25:29 PM
Old 09-15-2011
Please don't bump.

Quote:
Originally Posted by me_newbie
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:

Code:
for (i=1;i<=NF;i++) {arr[$i]++}

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.

Code:
echo asdf | awk '{ print }
{ print }'

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.


You can mix and match them however you like, i.e.

Code:
# Only run the code block if NR is 1.
# Only print the line if it matches /sd/.
echo asdf | awk 'NR==1 {} /sd/'

This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

searching for a string in directory

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)
Discussion started by: warrend
4 Replies

2. UNIX for Dummies Questions & Answers

searching by string length

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)
Discussion started by: GADO
4 Replies

3. Shell Programming and Scripting

one more query for searching string.......

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)
Discussion started by: swamymns
6 Replies

4. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

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)
Discussion started by: mohancrr
1 Replies

5. UNIX for Dummies Questions & Answers

Searching for a string variable

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)
Discussion started by: mattrix
2 Replies

6. UNIX for Dummies Questions & Answers

searching for a string in a file

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)
Discussion started by: ROOZ
2 Replies

7. Shell Programming and Scripting

searching each file for a string

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)
Discussion started by: Phuti
2 Replies

8. Shell Programming and Scripting

searching a text string for n'th :

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)
Discussion started by: sasglm
9 Replies

9. Shell Programming and Scripting

searching the required string and appending string to it.

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)
Discussion started by: firestar
6 Replies

10. Shell Programming and Scripting

Searching for variable string

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)
Discussion started by: hello_world
7 Replies
All times are GMT -4. The time now is 12:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy