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)
# 8  
Old 03-27-2014
Quote:
Originally Posted by vbe
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...
okey! ty, I'll probably be back alittle later, but this is something to go on Smilie

---------- Post updated at 09:37 AM ---------- Previous update was at 09:36 AM ----------

Quote:
Originally Posted by sea
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.
Will do!
# 9  
Old 03-27-2014
Try this

Code:
#!/bin/bash

addresses=/absolute_path_to_your_adresses_file/adresses.txt

read -p "Insert the word to search here : " word
grep -iq $word $addresses
if [ $? -eq 0 ]; then
grep -i $word $addresses
else
echo "the country/city you searched doesn;t exist in our list"
fi

you can edit the output format.
Also important I used 'grep -i' so the search for the country/city is done disregarding small or capitol letters. I mean if you type 'sweden' it will return also the line containing 'Sweden' .
if you don't like it, you can delete '-i' option
This User Gave Thanks to black_fender For This Post:
# 10  
Old 03-27-2014
Quote:
Originally Posted by black_fender
Try this

Code:
#!/bin/bash

addresses=/absolute_path_to_your_adresses_file/adresses.txt

read -p "Insert the word to search here : " word
grep -iq $word $addresses
if [ $? -eq 0 ]; then
grep -i $word $addresses
else
echo "the country/city you searched doesn;t exist in our list"
fi

you can edit the output format.
Also important I used 'grep -i' so the search for the country/city is done disregarding small or capitol letters. I mean if you type 'sweden' it will return also the line containing 'Sweden' .
if you don't like it, you can delete '-i' option
works like a charm man! ty

and thanks to everyone who took the time!
# 11  
Old 03-27-2014
Could be shorten to:
Code:
read -p "Insert the word to search here : " word

if ! grep -i $word $addresses ; then
     echo "the country/city you searched doesn;t exist in our list"
fi

Because if it success', it'll print the desired Output anyway.
 
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