Help with if then sentence (string in file)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with if then sentence (string in file)
# 1  
Old 03-27-2014
Help with if then sentence (string in file)

Hello! I'd like some help with a sentance, this 'if' should take a string from the user, then search my list for that string, now only those lines that string is found should be worked on.

I'm new to this, but i'm guessing it's something like this..

Code:
           #!/bin/bash
           OPTIONS="opt1 opt2 opt3"
           select opt in $OPTIONS; do
               if [ "$opt" = "opt1" ]; then
                # how can I search the list for 'opt1' ?
               elif [ "$opt" = "opt2" ]; then
                # or 'opt2' ..
               elif [ "$opt" = "opt3" ]; then
                # or 'opt3' ..
               else
                clear
                echo bad option
               fi
           done

Maybe there's something better than an if sentance for this?
The file is a list of countrys and citys, like this:

Code:
Country: Sweden, City: Stockholm
Country: Sweden, City: Malmö
Country: Norway, City: Oslo
Country: Norway, City: Bergen

Let's call this file 'addresses' and, the options in the script could be country names, like Sweden or Norway, and if the user choses 'Norway' we would copy all 'addresses' with Norway, as Country

I hope you understand, and can find some time to help me! (and please, if you have a solution, try and explain it alittle, as I said, i'm new.. )
# 2  
Old 03-27-2014
You should be using case statement for this...
# 3  
Old 03-27-2014
Quote:
Originally Posted by vbe
You should be using case statement for this...
hmm, okay, I will look at that. but it works almost the same way dont it?

and, how can I check the list, to see what's after "Country:" ?
# 4  
Old 03-27-2014
To tell the truth I cant link your bit of script with what you are asking...
Quote:
take a string from the user, then search my list for that string
I dont see that n your script...
Quote:
now only those lines that string is found should be worked on
So what are option1 ... then?
If you are searching in a file, then grep is what you should use to sart with, then once found think if awk should not be prefered depending on what actions should follow...
# 5  
Old 03-27-2014
Quote:
Originally Posted by vbe
To tell the truth I cant link your bit of script with what you are asking...

I dont see that n your script...

So what are option1 ... then?
If you are searching in a file, then grep is what you should use to sart with, then once found think if awk should not be prefered depending on what actions should follow...
I'm thinking, If option 1 is Norway, then when the user enters norway, everything in the list, that has the value 'Norway' as Country, gets copied to the new file.

What I need is a way to compare the users option with the value of 'country'.
Like
Code:
if [option1 = Country]; then
       copy to newlist

edit! and, it should do this on every line on the list, so maybe I need a for loop?
# 6  
Old 03-27-2014
Use getopts (the bultin one ; getopt is an external) to parse arguments.
Once got familiar, it is very powerfull and helps alot in error handling/fetching Smilie

Search this Forum about it, has quite a lot threads on this Topic.
# 7  
Old 03-27-2014
I suggest you write a script that accepts an input, lets call it OPT1, then grep that value in your file, and displays what is found.
Once that is achieved, You explain from the result what more you want...
So far for this script you need a read and grep , perhaps should we have an if if grep returns nothing...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I must use first sentence from a file to put into variable

i am having some bash script which must use first sentence of the file. For example i have file which content is: test 213 So I must use word test into my bash script, and put it into variable. I am using a one variable named value value=$(</home/rusher/test.txt) so instead using test.txt... (1 Reply)
Discussion started by: tomislav91
1 Replies

2. Shell Programming and Scripting

Extract sentence and its details from a text file based on another file of sentences

Hi I have two text files. The first file is TEXTFILEONE.txt as given below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (7 Replies)
Discussion started by: my_Perl
7 Replies

3. Shell Programming and Scripting

Print one sentence 40 to 50 words end with period in a file

Hi All, Is there another way to achieve this? how get short phrase in a sentence with character count of 100 to 155 words end with period but don't end something like 50,000. . Here's my current script but the output is not good. This will use for my snippets or preview. grep... (6 Replies)
Discussion started by: lxdorney
6 Replies

4. Shell Programming and Scripting

How to get a number from a grepped sentence of a file?

I want get a number(ID) from a sentence which has been grepped from file using error number. For Example: #!/bin/ksh echo "Enter RRS ID: " read rrs echo "Enter error number:" read err scp -pr ptc-avdbamdw102:/home/icsprd/M3logs/Accurate/logs/corp_post/$rrs.*.err.txt $HOME/daemon_mail/... (7 Replies)
Discussion started by: JayDoshi
7 Replies

5. Shell Programming and Scripting

[grep] how to grep a sentence which has quotation marks "sentence"

I would like to check with grep in this configuration file: { "alt-speed-down": 200, "alt-speed-enabled": true, "alt-speed-time-begin": 1140, "alt-speed-time-day": 127, "...something..." : true, ... } "alt-speed-enabled" (the third line of the file) is setted to... (2 Replies)
Discussion started by: ciro314
2 Replies

6. Shell Programming and Scripting

I have to pass a sentence in a file,

I have to pass a sentence in a file, the specs are as: cat run | sed 's/SRT/'$8'/g' | sed 's/plength/68/g' | sed 's/stcol/'$5'/g' | sed 's/encol/'$6'/g' | sed 's/brdtype/'$1'/g' | sed's/brdtxt/'$3'/g' | sed 's/demotxt/Total '$2'/g' | sed 's/bantxt/ban_'$7'/g' | sed 's/validcodes/'$4'/g' > runx ... (1 Reply)
Discussion started by: patilrakesh1984
1 Replies

7. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

8. Shell Programming and Scripting

Append a sentence in each file.

In a directry there are 100 files are present.... How to append a statement like "Anup Das" in each of the file content, in the first line.... without opening the files.... (2 Replies)
Discussion started by: anupdas
2 Replies

9. Shell Programming and Scripting

How to remove duplicate sentence/string in perl?

Hi, I have two strings like this in an array: For example: @a=("Brain aging is associated with a progressive imbalance between intracellular concentration of Reactive Oxygen Species","Brain aging is associated with a progressive imbalance between intracellular concentration of Reactive... (9 Replies)
Discussion started by: vanitham
9 Replies

10. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question