Nested if statements with word/number extracts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested if statements with word/number extracts
# 1  
Old 05-13-2014
Nested if statements with word/number extracts

Hi everyone!

I'm having difficulties setting up a complex script with nested if statements while doing some word extracts, any help is appreciated.

Scenario:

1- Check if the file.txt has the word BINGO
2- If so then get the available number (any number) in the line that contains the word "TARGET" into number.txt
3- Check if number within number.txt is bigger than number in file basenumber.txt
4- If so, echo "Your BINGO number is bigger!"
5- If not, delete all files.

file.txt = "Header BINGO

You will check and take action ASAP.

Please go and compute the ticket 0550010 as it is the target.

Thanks"

basenumber.txt = "0220050"

Code:
if [ dump.txt =~ "BINGO"]
 then grep "TARGET" | "extract only the number in this line, everything else is not required" > number.txt
    elif [number.txt > basenumber.txt]
      then echo "Your BINGO number is bigger!"
        else rm *.txt
          fi
 else rm *.txt
  fi

Thanks a lot!

Last edited by demmel; 05-13-2014 at 03:23 PM..
# 2  
Old 05-13-2014
Here are the files that you need. My name is Brian.
You should know that so you can add my name to
your assignment when you turn it in.

Smilie


Code:
$ cat ./bingo.sh
#!/bin/bash

if [ $# -lt 2 ]
then
echo ""
echo " ERROR : Invalid number of arguments"
echo " Usage : base_file, dump_file"
exit 1
fi

export BASE_FILE=$1
export DUMP_FILE=$2
export BASE_NUMBER=`head -1 ${BASE_FILE}`

if [ `grep -c BINGO ${DUMP_FILE}` -gt 0 ]
then
   export TARGET_NUMBER=`grep BINGO ${DUMP_FILE} | cut -d" " -f2`
   if [ "${TARGET_NUMBER}" -gt "${BASE_NUMBER}" ]
   then
      echo "Your BINGO number is bigger!"
   else
      echo "Your number is not bigger!"
   fi
fi

# rm -v ${DUMP_FILE}

$ cat base.txt
5
$ cat dump.txt
INGO 001
INGO 001
INGO 001
INGO 001
BINGO 999
INGO 001
INGO 001
INGO 001
INGO 001
INGO 001

This User Gave Thanks to gandolf989 For This Post:
# 3  
Old 05-13-2014
Thanks!

Last edited by demmel; 05-13-2014 at 05:04 PM.. Reason: Thanks!
# 4  
Old 05-13-2014
grep, like all shell utilities, returns a code if it succeeds or fails. You don't need to count the number of lines it prints to tell whether it found anything. You can feed it directly into an if-statement and use less processes for the same result.

Code:
if grep "string" filename >/dev/null
then
        echo "string found in filename"
else
        echo "string not found in filename"
fi

# 5  
Old 05-13-2014
Quote:
Originally Posted by gandolf989
Here are the files that you need. My name is Brian.
You should know that so you can add my name to
your assignment when you turn it in.

Smilie


Code:
$ cat ./bingo.sh
#!/bin/bash

if [ $# -lt 2 ]
then
echo ""
echo " ERROR : Invalid number of arguments"
echo " Usage : base_file, dump_file"
exit 1
fi

export BASE_FILE=$1
export DUMP_FILE=$2
export BASE_NUMBER=`head -1 ${BASE_FILE}`

if [ `grep -c BINGO ${DUMP_FILE}` -gt 0 ]
then
   export TARGET_NUMBER=`grep BINGO ${DUMP_FILE} | cut -d" " -f2`
   if [ "${TARGET_NUMBER}" -gt "${BASE_NUMBER}" ]
   then
      echo "Your BINGO number is bigger!"
   else
      echo "Your number is not bigger!"
   fi
fi

# rm -v ${DUMP_FILE}

$ cat base.txt
5
$ cat dump.txt
INGO 001
INGO 001
INGO 001
INGO 001
BINGO 999
INGO 001
INGO 001
INGO 001
INGO 001
INGO 001


Thanks Brian from Pittsburgh AKA gandolf989.

This is actually part of a solution for daily manual intervention that I face. It will surely help me a great deal. I'll make sure to put your nick and domain as author as well! (nobody will start contacting you so your fine LOL)

I'm still trying to understand all the variable that you set up, however I can thank you already.


Code:
if [ $# -lt 2 ] then echo "" echo " ERROR : Invalid number of arguments" echo " Usage : base_file, dump_file" exit 1 fi

The return code mapping 2 was helpful. OK got that.

export BASE_FILE=$1 export DUMP_FILE=$2 export BASE_NUMBER=`head -1 ${BASE_FILE}`I still couldn't understand how these variables run into place.
Maybe its the parameter I run within the command?
"/path/bingo.sh base.txt dump.txt"



Code:
if [ `grep -c BINGO ${DUMP_FILE}` -gt 0 ]

Cool, got that.

Code:
export TARGET_NUMBER=`grep BINGO ${DUMP_FILE} | cut -d" " -f2`

Would the number extract parameter work only if its present in column 2, being separated by space? If not, its no big deal, I can arrange the correct column as dump file will always be with the same columns.

Code:
if [ "${TARGET_NUMBER}" -gt "${BASE_NUMBER}" ]

Great

Sounds good, I'll start some testing!

Thank you very much!
# 6  
Old 05-13-2014
Quote:
Originally Posted by demmel
Code:
if [ $# -lt 2 ] then echo "" echo " ERROR : Invalid number of arguments" echo " Usage : base_file, dump_file" exit 1 fi

The return code mapping 2 was helpful. OK got that.
$# is not the return code, $# is the number of arguments.

Quote:
export BASE_FILE=$1 export DUMP_FILE=$2 export BASE_NUMBER=`head -1 ${BASE_FILE}`I still couldn't understand how these variables run into place.
$1 is the first argument, base.txt. $2 is the second argument, dump.txt. BASE_NUMBER is the first line from base.txt

Quote:
Code:
if [ `grep -c BINGO ${DUMP_FILE}` -gt 0 ]

Cool, got that.
I'd rewrite that as if grep -l BINGO "${DUMP_FILE}" >/dev/null, no need to count lines, just use grep's return code.
# 7  
Old 05-13-2014
Quote:
Originally Posted by Corona688
I'd rewrite that as if grep -l BINGO "${DUMP_FILE}" >/dev/null, no need to count lines, just use grep's return code.
Doesn't the grep need to be in back ticks? ``Smilie
There is always another way to do anything with shell scripting. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

2. Shell Programming and Scripting

GPS extracts fix delta time

Hello all, I am currently trying to find the delta time from some GPS log. I am using the following script with awk. But the script result shows some incorrect values (delta time some time = 0.2 but when I check it manually it is equal to 0.1) My final goal is to get a script that print... (7 Replies)
Discussion started by: redafenj
7 Replies

3. Shell Programming and Scripting

Print only word not number

Hi, Need to extract only words not numbers #cat test.txt 123456 oracle web 56789 s21adm Required output #grep <options> test.txt oracle web s21adm Note, in between integer "s21adm" is required but not with full integer "123456" and "56789" (6 Replies)
Discussion started by: ksgnathan
6 Replies

4. Shell Programming and Scripting

Nested awk Statements

Hello again everyone, yes, I'm back again for more help! So I'm attempting to read two separate files and generate some XML code from that. My current code is: BEGIN { print "<?xml version=\"1.0\" encoding=\"utf-8\">" print "<Export>" } { x=1; print "<section name=\"Query" NR "\">"... (5 Replies)
Discussion started by: Parrakarry
5 Replies

5. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

6. UNIX for Advanced & Expert Users

Extracts files names and write those to another file in different directory

Hi , Need to shell script to extracts files names and write those to another file in different directory. input file is inputfile.txt abc|1|bcd.dat 123 david123 123 rudy2345 124 tinku5634 abc|1|def.dat 123 jevid123 123 qwer2345 124 ghjlk5634 abc|1|pqr.txt 123 vbjnnjh435 123 jggdy876... (9 Replies)
Discussion started by: dssyadav
9 Replies

7. Shell Programming and Scripting

Nested looping statements

I cannot get the code below to work correctly. The IF statement works just fine, but not the looping. The inner loop tries to find files for a given vendor; if found, I need to sleep giving another process time to move the files. Once the given vendor's files are gone, then I want to move on to the... (1 Reply)
Discussion started by: dgreene
1 Replies

8. Shell Programming and Scripting

syntax question in regards to nested awk statements

Hello all, I am writing up an input file and I was hoping I could get some guidance as to how to best consolidate these 2 awk statements for 1 while loop. Here's my input file # cat databases.lst #NOTE: These entries are delimited by tabs "\t" #oracleSID name/pass # db11 ... (2 Replies)
Discussion started by: Keepcase
2 Replies

9. Shell Programming and Scripting

Nested if statements with && and ||?

Hello, I'm a shell scripting noob and new to this forum as well. My question is can nested if statements be done with && and || instead and if it can be done can someone provide an example pls. Thanks in advance for the help (1 Reply)
Discussion started by: zomgshellscript
1 Replies

10. Shell Programming and Scripting

How to have a Script that extracts files?

Hi, I know there is a way to have a script that include files in a way that once we run the script, it would extract those files. Kinda similar to how zip files work. For example: - I have a script called test.sh - Once I run this script, it will extract file1, file2 and file3 and then... (2 Replies)
Discussion started by: pgarousi
2 Replies
Login or Register to Ask a Question