Sponsored Content
Top Forums Shell Programming and Scripting How to use Bash and awk to search in a file? Post 302616701 by bashily on Sunday 1st of April 2012 09:18:23 AM
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"

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
httppower(8)							     powerman							      httppower(8)

NAME
httppower - communicate with HTTP based power distribution units SYNOPSIS
httppower [--url URL] DESCRIPTION
httppower is a helper program for powerman which enables it to communicate with HTTP based power distribution units. It is run interac- tively by the powerman daemon. OPTIONS
-u, --url URL Set the base URL. INTERACTIVE COMMANDS
The following commands are accepted at the httppower> prompt: auth user:pass Authenticate to the base URL with specified user and password, using ``basic'' HTTP authentication which sends the user and password over the network in plain text. seturl URL Set the base URL. Overrides the command line option. get [URL-suffix] Send an HTTP GET to the base URL with the optional URL-suffix appended. post [URL-suffix] key=val[&key=val]... Send an HTTP POST to the base URL with the optional URL-suffix appended, and key-value pairs as argument. FILES
/usr/sbin/httppower /etc/powerman/powerman.conf ORIGIN
PowerMan was originally developed by Andrew Uselton on LLNL's Linux clusters. This software is open source and distributed under the terms of the GNU GPL. SEE ALSO
powerman(1), powermand(8), httppower(8), plmpower(8), vpcd(8), powerman.conf(5), powerman.dev(5), powerman-devices(7). http://sourceforge.net/projects/powerman powerman-2.3.5 2009-02-09 httppower(8)
All times are GMT -4. The time now is 05:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy