Searching word in a file with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching word in a file with awk
# 1  
Old 07-09-2008
Searching word in a file with awk

Hello everyone!

I use a script to query for installed packages with yum (I use RHEL 4 with yum installed) and the output is redirected to a file. My script scan this file to find a package and the version of it.

The script works fine until I search for a package name with special characters. So if I search for gcc.x86_64 I get all the info I need but if I search for gcc-c++.x86_64. I don't get anything.

My search string looks like this :
Code:
version=$(awk '/^gcc.x86_64/ { print $2}' /tmp/yum_output.txt)

The ouput of yum is :
Code:
Setting up repositories
Reading repository metadata in from local files
Installed Packages
gcc.x86_64                               3.4.6-3                installed


So if I echo version, I get this :
Code:
# version=$(awk '/^gcc.x86_64/ { print $2}' /tmp/yum_output.txt)
# echo $version
3.4.6-3
#

When I do the same thing with gcc-c++.x86_64, I get nothing if I echo version.
Code:
# version=$(awk '/^gcc-c++.x86_64/ { print $2}' /tmp/yum_output.txt)
# echo $version

#


Is there any way I can get this to work?

Last edited by radoulov; 07-09-2008 at 12:23 PM.. Reason: please use code tags
# 2  
Old 07-09-2008
Try escaping the plus sign:

Code:
version=$(awk '/^gcc-c\+\+\.x86_64/ { print $2}' /tmp/yum_output.txt)

# 3  
Old 07-09-2008
Try to escape the plus sign:
Code:
version=$(awk '/^gcc-c\+\+.x86_64/ { print $2}' /tmp/yum_output.txt)

# 4  
Old 07-09-2008
Thanks a lot! works perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with the awk in searching the contents in a file

Hello all, I am a newbie in awk. I am struggling in this problem for a long.Actually I have two files, filea and fileb. File a is actually a search key through it I have to find the corresponding japanese tag from file b. filea contains the data like this: sm982882 sm1893548 sm2420025... (3 Replies)
Discussion started by: csim_mohan
3 Replies

2. Shell Programming and Scripting

Searching a file using awk or sed

I want to search a file in a specific location and I don't want to use find command. I want to give the path also where the file is for searching it. Pls help (3 Replies)
Discussion started by: maitree
3 Replies

3. Shell Programming and Scripting

Searching a word in multiple files

Hi All, I have a issue in pulling some heavy records , I have my input file has 10,000 records which i need to compare with daily appended log files from (sep 1st 2009 to till date) . I tried to use grep fgrep and even sed , but the as time is factor for me , i cannot wait for 5 days to get the... (3 Replies)
Discussion started by: rakesh_411
3 Replies

4. Shell Programming and Scripting

Insert tags in lines after searching for a word

Hi, I am new to Unix and this is my first post on this forum. I am trying to convert a file into an xml. In my input I want to search for any line that starts with a 'F' and make it a tag in the xml. See below for the input and output. Input : <Payment> <REFERENCE>78</REFERENCE> F123 : ... (7 Replies)
Discussion started by: akashgov
7 Replies

5. Shell Programming and Scripting

searching in a while where a word is not there.

Hi, I am new to unix, pls help I have a file suc_Logfile file. liek this. output success success abc Now i need to generate a file in shell script where it shows only abc. Now i am doing like this grep "^successfully\|$" $suc_Logfile >> $Final_Suc Pls help. Thanks,... (6 Replies)
Discussion started by: sailaja_80
6 Replies

6. UNIX for Dummies Questions & Answers

Searching mutiple word - Tuning grep command

Hi all, I have a log file which is more than 1GB, i need to take count from the log file for two strings. i am using the below command but it take a long time to excetue, i need to tune this. Please help me cat /logs/gcbs/gcbsTrace.log | grep -i "ViewStatementBusinessLogic" | grep -c -i... (8 Replies)
Discussion started by: senthilkumar_ak
8 Replies

7. Shell Programming and Scripting

Reading an Input file and searching for occurrences WIHOUT SED or AWK

Hi people. I am new to shell scripting, so I need a little help. I want to create a script named that takes an argument as a file, Read the input file and look for occurrences of the current username (say abc.xyz) who is executing the script. On finding an occurrence of the username take that line... (2 Replies)
Discussion started by: kartikkumar84@g
2 Replies

8. Shell Programming and Scripting

problem while searching in a file by 'AWK'

Hi , i am writing a script in which i am using following command to grep some string and then storing it's output to v, as like below :- v=$(awk -F, '{ if ( $NF ~ /DEV/ ) print $0 "_BLD01";else print $0 "_RC01" }' mytest) Here i am facing following issues:- 1. it is searcing DEV in the... (1 Reply)
Discussion started by: inderpunj
1 Replies

9. UNIX for Dummies Questions & Answers

searching word in files

hi all i want to search some word in file but i want that it will search in all the files that i have in all the directories i do'nt know the name of the file i know that grep command want some specified file in which command shell i used thanks (2 Replies)
Discussion started by: naamas03
2 Replies

10. UNIX for Dummies Questions & Answers

Searching for key word within a file

Hello, I have a directory that holds all of my matlab codes. I am trying to run a searh on all of the matlab files that have the word "while" written inside the sytax of the code. Looking for all of the times that I did a while loop. Can someone help me do this? Thanks in advance. ... (1 Reply)
Discussion started by: moradwan
1 Replies
Login or Register to Ask a Question