Searching a String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching a String
# 8  
Old 09-15-2011
Hello Guys !

i'm new to shell so please if anyone could explain the following things in detail

1> F"[,.?\! ]" why we used it? and what all these sign (,.?\!) means here can we add more?
2> what are the two for loops used for and how they work?
3> what is NF and END?

thank you guys

BR,
Ewa

---------- Post updated at 01:32 PM ---------- Previous update was at 07:23 AM ----------

Hello Guys !

i'm new to shell so please if anyone could explain the following things in detail

1> F"[,.?\! ]" why we used it? and what all these sign (,.?\!) means here can we add more?
2> what are the two for loops used for and how they work?
3> what is NF and END?

thank you guys

BR,
Ewa
# 9  
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  
Old 09-16-2011
Java

Hi Corona !

thanks dear you explained it in a very detailed level, help appreciated... i'm working with a similar problem from two days if i couldn't solve, i'll need help of you guys can i post it here because it is very simple in nature

---------- Post updated at 08:44 AM ---------- Previous update was at 03:56 AM ----------

Hi Guys !

i couldn't made it so here is my question

i am only interested in the first column of each line and it should check how many times string or numerical value is repeated in the first column in the whole file

for example

he is a good boy
he goes to school
2 is less then 3
he is very hard working
she is from NY
she lives with her parents
they all are playing
he is from NJ
2 and 2 makes four
2 is greater than 1

produce an output in asceding order regarding with such format as given below

Subject - Repeated
he - 4
2 - 3
she - 2
they - 1


is it possible using nawk or awk?


Thanks in advance

BR

Ewa

Last edited by me_newbie; 09-16-2011 at 11:20 AM..
# 11  
Old 09-16-2011
That's just a simplified version of what you already did...

Code:
awk '{ C[$1]++; }
END { for(K in C) print K, C[K]; }'

This User Gave Thanks to Corona688 For This Post:
# 12  
Old 09-16-2011
Hi Corona !

Thanks... your help is appreciated your solution worked

cheers!

BR

Ewa

Last edited by me_newbie; 09-18-2011 at 08:15 AM..
# 13  
Old 09-25-2011
hi,
I know you've already got a solution but just to say there is another way which gives you pretty much the same output you wanted:
Code:
grep -o '^\<[[:alnum:]]*\>' yourtextfile  | sort | uniq -c | sort -nr

* grep -o '^\<[[:alnum:]]*\>' finds the first alphanumeric word (using \< and \>, see the grep manpage for an explanation) on the line. If you remove the "^" it will give you word frequency for every word in the file.
* the option -o means only print the matched pattern, which is often extremely useful
So you've already now extracted all the first words from your file, you just have to count the frequency of them, which you can do with uniq -c after they're ordered so duplicates are adjacent. The final sort -r (reverse order) puts them in the ordered you wanted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question