How to use Bash and awk to search in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use Bash and awk to search in a file?
# 1  
Old 04-01-2012
How to use Bash and awk to search in a file?

I have the following code as a file which a user can choose with an option to search in that file. The file name is not specified and can have any name and extension. Now a sample of this file is like this:
Code:
1,root,init,20,0.0,0.1,0:01.78
1008,root,migration/0,1,2.0,1.8,7:04.32
5,root,watchdog/0,1,0.0,0.0,0:00.00

I know that I have to use awk or sed and get the result printed in a table format? user may choose a combination of switches as well.
This is the code that I have:

Code:
while getopts ":u:c:f:tmp" opt;do
 case $opt in
        u)      user=$OPTARG;awk '/'$user'/{FS=","; print}' < $(($OPTIND + 1));;
        c)      command=$OPTARG;;
        f)      fileName=$OPTARG;;
        t)      totalTime=$OPTARG;;
        m)      memoryUse=$OPTARG;;
        p)      priority=$OPTARG exit;;
        \?)     usage;exit 1;;
        *)      echo "Option -$OPTARG requires an argument.";exit 1;;
 esac
done
shift $(($OPTIND - 1))

any idea??!?
# 2  
Old 04-01-2012
Hi,

Your input file does not seem to have a consistent format around the "nice" field.

You can read and process your file easily with a while read loop:
Code:
while IFS="," read field1 field2 field3 field4 ...
do
  ....
done < infile

This is more elegant than using sed, or awk and if you plan to use either within the read loop, this will certainly be the more efficient and faster option...

Regarding you options segment, did you check the answers to one of your other threads?
# 3  
Old 04-01-2012
I should say thanks to you "Scrutinizer" I got some of the code correctly; however, the loop that you were suggesting, wouldn't work with my code. The reason is: I don't know the name of the file which user checks for date. Above that, user can have a different combination of switches! As a result, I am not able to use $# to get the position. I hope it is clear.

And yes I fixed what you gave me here.

---------- Post updated at 09:18 AM ---------- Previous update was at 09:06 AM ----------

Let say I need to check in a text file that I don't know the name within different options in the following code. How do you get the file to search for regular expressions which user has typed in the command line? I am not a professional, I apologize for silly mistakes and bad explanation.
Code:
#!/bin/bash
# Argument = -t test -r server -p password -v
# Argument = -h

function usage()
{
cat << EOF
usage: $0 options

OPTIONS:
   -h      Show this message
   -t      Test type, can be 'test1' or 'test2'
   -r      Server address
   -p      Server root password
   -v      Verbose
EOF
}

theTest=
theServer=
thePassword=
verbose=
while getopts ":hvt:r:p:" x
do
     case $x in
         h)
             usage
             exit 1
             ;;
         t)
             theTest=$OPTARG
             ;;
         r)
             theServer=$OPTARG
             ;;
         p)
             thePassword=$OPTARG
             ;;
         v)
             verbose=1
             ;;
         ?)
             usage
             exit
             ;;
     esac
done

if [[ -z $theTest ]] || [[ -z $theServer ]] || [[ -z $thePassword ]]
then
     usage
     exit 1
fi
if [ "$verbose" != "" ]; then
    echo Here are the options
    echo "the value for -t is $theTest"
    echo "the value for -r is $theServer"
    echo "the value for -p is $thePassword"
fi
echo "the value for -v is $verbose"

# 4  
Old 04-01-2012
Hi, glad that you found useful. You could use the while-read loop after you find out about the file:
Code:
while IFS="," read field1 field2 field3 field4 ...
do
  ....
done < "$fileName"

After you are done with getopts, you can do
Code:
shift $(($OPTIND - 1))

as I explained in the other thread.

The filename should then be in $1 and $# should be equal to 1, if the user specified one parameter to the script, besides the command line options...

I would suggest you play around a little with the code to verify these things...

*EDIT*

Just saw your latest installment of your script, and suddenly there is a root password, a server IP address and a regular expression on the command line? I am really not sure what direction you are heading..

Last edited by Scrutinizer; 04-01-2012 at 10:54 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-01-2012
That code is an example that I fount on the web. I'll do my best to figure out what I want with your assistance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to search file for string and lauch function if found

In the bash below I am searching the filevirus-scan.log for the Infected files: 0 line (in bold) and each line for OK. If both of these are true then the function execute is automatically called and processing starts. If both these conditions are not meet then the line in the file is sent to the... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Bash loop to search file

In the bash when the user inputs an id to search for the bash currently closes, and if a match is found outputs a new file (match.txt). Is it possible to have not close the bash but rather, on the screen "searching for match" and if a match is found "match found in line.." is displayed... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

Search info about awk bash instruction

hello i have search in an old .bash_history and i find this instruction : xhost `xhost | awk -F: 'NR > 1 {printf ("-%s ", $NF)}'` someone give me some informations about this instruction?:confused: thx (2 Replies)
Discussion started by: overflow
2 Replies

5. Shell Programming and Scripting

Search strings and highlight them using Perl or bash/awk/sed

Hi, I have two files: a.doc and b.txt I wish to search the strings from file b.txt in a.doc and want to highlight them in a.doc with different colours using Perl or bash./awk/sed? Please guide me. :) Thanks!!!!! (10 Replies)
Discussion started by: bioinfo
10 Replies

6. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

7. Shell Programming and Scripting

bash script search file and insert character when match found

Hi I need a bash script that can search through a text file and when it finds 'FSS1206' I need to put a Letter F 100 spaces after the second instance of FSS1206 The format is the same throughout the file I need to repeat this on every time it finds the second 'FSS1206' in the file I have... (0 Replies)
Discussion started by: firefox2k2
0 Replies

8. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

9. Shell Programming and Scripting

Bash Script Help...search then read from file: change text and pipe back...

Hello, I am trying to make a bash script that can pull data from a file and then change one part of said data. I want to search by username and pull the full line. That way there is a way to replace just one part of that line then return it back to the file. My Data is stored like: ... (1 Reply)
Discussion started by: serverfull
1 Replies

10. UNIX for Dummies Questions & Answers

How to search a file in bash terminal?

Hi all, I am new to unix. Anyone knows how to search a file in bash terminal, please kindly tell me. For example, I want to search file named "myfile" in the entire machine... Thanks a lot:) (2 Replies)
Discussion started by: andrewust
2 Replies
Login or Register to Ask a Question