Searching for values in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for values in a file
# 1  
Old 01-12-2013
Searching for values in a file

Hi guys.

I'm trying to do a search on the fruit & brand inside Fruit.txt, and printing the result out in the following format:

[fruit], [brand], $[price], [qty]

I am able to do this via the following code:

Code:
awk -F: -vOFS=", " -vt="$Fruit:$Brand" '$0~t{$3="$"$3;print}' Fruit.txt

However, I want to be able to search only one word anywhere on the string (case insensitive), and it will still be able to return me the results.

Example as follows:
Code:
Enter fruit name: sour
Enter brand: [null]

Found 2 matches:
Sour Grapes, Sunshine Ltd, $0.95, 200
Sour Apples, Moonlight Ltd, $0.30, 100

---------------------------------------
Enter fruit name: sour
Enter brand: moonlight

Found 1 match:
Sour Apples, Moonlight Ltd, $0.30, 100

How do I go about putting the search conditions?
# 2  
Old 01-12-2013
The easiest way might be to make the t variable a regex: -vt="$Fruit.*$Brand"It will find the lines with both strings present (Fruit before Brand!), or, if one is missing, all the lines having the other value.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-12-2013
Sorry, I haven't tried the code yet, but if let's say I were to find "Apples", will I be able to get back:
Quote:
Sour Apples, Moonlight Ltd, $0.30, 100
# 4  
Old 01-12-2013
Surprising question. Just try! I'm pretty confident it will.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-12-2013
I tried the code. It seems to be able to work.

However, I just tried to search in lowercase, but it didn't return any results.

Also, if one search term is missing, but the other is present, the result will be "Please fill in the necessary information.".

Code:
if test "$Fruit" = "" || test "$Brand" = ""; then
			echo "Please fill in the necessary information."
		else
			awk -F: -vOFS=", " -vt="$Fruit.*$Brand" '$0~t{$3="$"$3;print}' Fruit.txt
		fi

# 6  
Old 01-12-2013
awk works case sensitively. If you want to match upper and lower case chars, special action has to be taken, like applying the toupper/tolower functions. Or modify the regex accordingly (hint: [aA]).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Taking key values from one file and extracting values from another file

Hi, I have two files with values in both. File1: cat 2 3 dog 4 5 elephant 6 7 camel 2 3 File2: ----+--gkf;ajf= ---+---- +----- cat -------=----+ 3 | 4 ----- dog ------++-- 5 | 9 ----++-- elephant | 5 | 7 ---++ camel ------ ++++_---- || 8 | 9 I want the final file as: cat 4... (1 Reply)
Discussion started by: npatwardhan
1 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. UNIX for Dummies Questions & Answers

Need help searching for values in file then adding to line

Hello! I'm currently trying to organize data for some bio research, but I'm not sure how to compare a value to values in a file. So what I have are 2 arrays, one array contains NM numbers and can be referenced as NM. The other array has symbols, SYM. I have a file for which it contains an NM... (1 Reply)
Discussion started by: ShiGua
1 Replies

5. Shell Programming and Scripting

Searching columns and subtracting values in awk

Hi everyone, I had a similar question a couple days ago but my problem has gotten significantly (to me anyway) more complex. I have two files: File 1: 0808 166 166 62 9 0 1000fights 1 1 2 1 0 100places2visit 2 2 2 2 0 10veronica91 167 167 3 1 0 11thgorgeous 346 346 3806 1461 122... (2 Replies)
Discussion started by: collards
2 Replies

6. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

7. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

8. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

9. Programming

searching files for hex or oct values

I have a set of files without extensions. How can I programatically tell if a file is in gzip format? The gzip file format spec RFC 1952 GZIP File Format Specification version 4.3 states that gzip files have certain hex/oct values at the beginning of the file. 1st byte = 0x1f in hex,... (2 Replies)
Discussion started by: daflore
2 Replies

10. Shell Programming and Scripting

Searching data files for another file of values

I've used awk for some simple scripting, but having trouble figuring out how to search a couple of data files that have Name/Address/Zip Codes from another file that has list of only Zip Codes, and write out the lines that matched. Zip code field in the data file is 27 I was thinking... (5 Replies)
Discussion started by: matkins99
5 Replies
Login or Register to Ask a Question