Replacing grep with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing grep with awk
# 1  
Old 07-21-2012
Replacing grep with awk

how to replace grep with awk commands.
i have a code like:
Code:
grep -w $student $markfile|$subjectid

can similar code be written by replacing grep with awk.

Thanks in advance,
regards,
giri

Last edited by Franklin52; 07-21-2012 at 07:49 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-21-2012
What are you trying to accomplish?? Smilie
# 3  
Old 07-21-2012
awk

Hi,
Welcome to unix.com!!!
Try this one,
Code:
awk -v w="${student}" '$0~w{print;}' ${markfile}

If you want to use bash variable inside awk, you have to use -v option or assign it before specify file.
Cheers,
Ranga:-)
This User Gave Thanks to rangarasan For This Post:
# 4  
Old 07-21-2012
Thanks Ranga,
while searching on the above query..I was able to find a solution..but with limitted success.

Code:
studentMarkInParticularSubject=`awk -F: '/$student/ ' $markfile | awk -F: '/$subjectid/ '`

I was getting results when i ran the code in the command prompt with value substituted
ie

Code:
studentMarkInParticularSubject=`awk -F: '/20/ ' classmark | awk -F: '/ENG/ '`

Code:
studentMarkInParticularSubject=`awk -F: '/$student/ ' $markfile | awk -F: '/$subjectid/ '`

Kindly , help me with this.
Thanks & Regards,
Giri

Last edited by girijan; 07-21-2012 at 10:50 PM.. Reason: added code tags
# 5  
Old 07-22-2012
Advice for forum posts, general:

To obtain the best answers quickly for processing datasets --
extracting, transforming, filtering, you should, after having
searched for answers (man pages, Google, etc.):

1. Post representative samples of your data (i.e. data that
should "succeed" and data that should "fail")

2. Post what you expect the results to be, in addition to
describing them. Be clear about how the results are to be
obtained, e.g. "add field 2 from file1 to field 3 from file2",
"delete all lines that contain 'possum', etc.

3. Post what you have attempted to do so far. Post scripts,
programs, etc. within CODE tags. If you have a specific
question about an error, please post the shortest example of the
code, script, etc. that exhibits the problem.

4. Place the data and expected output within CODE tags, so that
they are more easily readable.

Special cases, exceptions, etc., are very important to include
in the samples.

Last edited by Scrutinizer; 07-22-2012 at 02:44 AM..
# 6  
Old 07-22-2012
Try like this:
Code:
studentMarkInParticularSubject=`awk -F: '/'"$student"'/' $markfile | awk -F: '/'"$subjectid"'/'`

or like this:
Code:
studentMarkInParticularSubject=`awk -F: '$0 ~ '"$student" $markfile | awk -F: '$0 ~ '"$subjectid"`

Nevertheless, the 2 awk commands can be combined together. But that depends on your requirement.
This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 07-22-2012
Thanks elixir_sinari,
The solution was working when i tried in my script.
Thanks a lot for your time and efforts.

regards,
GiriSmilie

---------- Post updated at 09:13 PM ---------- Previous update was at 09:11 PM ----------

Thanks elixir_sinari,
The solution was working when i tried in my script.
Thanks a lot for your time and efforts.

regards,
giri
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Double grep and replacing

Hi All, I need to replace a match with are new values when there is a double match. grep 04422494 MAX.dat | grep 1102300131 I need to replace 1102300131 as 1101600131 in MAX.dat only for the record matching 04422494 . I tried replace as sed/s/1102300131 /1101600131 /g MAX.dat ... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. UNIX for Dummies Questions & Answers

Grep and replacing with ..

Hi I'm trying trying to replace '~/Documents/research/racedata' with '..' in all files in a directory. What is the best way I should go about this? Thanks. (4 Replies)
Discussion started by: Jane Yi
4 Replies

3. UNIX for Dummies Questions & Answers

Replacing string with awk

I am trying to replace javascript:top.showarticle('geo',1985,'with ../../abstracts/geo1985/I am using awk but getting an error awk -v s="javascript:top.showarticle('geo',1985,'" -v d="../../abstracts/geo1985/" '{gsub(s,d); print} ' geo50n12.html > geo50n12.html.tmp Fixed it using ... (3 Replies)
Discussion started by: kristinu
3 Replies

4. UNIX for Dummies Questions & Answers

Replacing grep with awk

This may be a repetitive or simple question, but I have a long running code, which uses a grep with a big argument, and i was wondering if there was a way to replace all of my greps with awk....here are my two commands using greps. x=$(ls /my/directory/here | grep -h ".$variable") and ... (1 Reply)
Discussion started by: jgrosecl
1 Replies

5. Shell Programming and Scripting

inserting and replacing lines with awk

Hello, I need to insert varying lines (i.e. these lines are an output of another script) between lines starting with certain fields. An example to make it more clear. This is the file where I wanna insert lines: (save it as "input.txt") ContrInMi_c_mir 2 10066 181014 200750... (12 Replies)
Discussion started by: tempestas
12 Replies

6. Shell Programming and Scripting

Replacing / with a space using awk

I have a string and want to replace the / with a space. For example having "SP/FS/RP" I want to get "SP FS RP" However I am having problems using gsub set phases = `echo $Aphases | awk '{gsub(///," ")}; {print}'` (5 Replies)
Discussion started by: kristinu
5 Replies

7. Shell Programming and Scripting

hexadecimal replacing with awk ?

Hi there ! I have text files with some nonsense characters in it, so different text editors put different nonsense symbols, and, worse, the application that should be able to read these files doesn't. With xxd, the nonsense characters show as "efbfbd", while they should be "c2a7" (the... (2 Replies)
Discussion started by: jossojjos
2 Replies

8. Shell Programming and Scripting

replacing characters with awk

Hi there! I'm new in this and probably is a quite simple task but I still cannot manage to do it: I have to read some files line by line and then change the input format into another one, but the very first step is to replace the empty variables by error values. I mean, each line looks like:... (2 Replies)
Discussion started by: thoreman
2 Replies

9. Shell Programming and Scripting

Specifying and replacing fields with awk

#cat BATCH007.TXT 01,661060052,061000104,081118,0915,07,80,1,2/ 99,,,2/ I have this file called BATCH007.TXT. I am trying to change fields 2 and 3 on line 2 to have zeroes. Like this: 01,661060052,061000104,081118,0915,07,80,1,2/ 99,0,0,2/ I can use these commands to print identify the... (2 Replies)
Discussion started by: ddurden7
2 Replies

10. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies
Login or Register to Ask a Question