How to take values from a file.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to take values from a file.?
# 1  
Old 05-28-2015
How to take values from a file.?

Hi All,

As of now I am checking for a sring using hard coded values in all_data.txt.
Instead of that I have to check using from the file.

Code:
awk '$1 ~ /COMS|SMAS|MARC/ {print $1, $2, $3 > "file_" $1}' all_data.txt

The below file is having the list.

Code:
client_list
COMS
SMAS
MARC

Please help me.

Thanks.
# 2  
Old 05-28-2015
Try

Code:
awk 'NR==FNR {a[$0]=$0;next} ($0 in a) {print $0 > "file_" $0}' pattern_file all_data.txt

# 3  
Old 05-28-2015
Take a look at the below link for at least one approach you can take:

Match or no match
# 4  
Old 05-28-2015
clx and mjf,
Note that $1 in a and the discussion in the referenced thread provide a test for exact matches, but $1 ~ /COMS|SMAS|MARC/ is testing for substring matches. In other words, if the first field in the input contains the substring COMS or the substring SMAS or the substring MARC, it will match even if it is not one of those three exact strings.

To get what was requested here, we would need something more like:
Code:
awk '
FNR == NR {
	e = (FNR == 1) ? $1 : (e "|" $1)
	next
}
$1 ~ e {if($1 in f)
		print $1, $2, $3 >> f[$1]
	else {	f[$1] = "file_" $1
		print $1, $2, $3 > f[$1]
	}
	close(f[$1])
}' client_list all_data.txt

Note that the close() is needed because since it is performing a substring match, even if there are only three lines in client_list, there could be thousands of output files created.

Of course, it is entirely possible that ROCK_PLSQL wants exact matches, but that is not what the code supplied in post #1 in this thread does.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 05-29-2015
Don,
I completely missed that subtlety on substring matches!

This solution appears to work as well:

Code:
$cat all_data.txt                                                                           
COMS 1111 AAAA
SMAS 2222 BBBB
MARC 3333 CCCC
MARCX 4444 DDDD
XCOMS 5555 EEEE
YYYY 6666 FFFF COMS
ZZZZ 7777 GGGG

Code:
$cat pattern_file                                                                           
COMS
SMAS
MARC

Code:
$nawk 'NR==FNR {a[$1];next} {for (i in a) if ($1 ~ i) {print $0}}' pattern_file all_data.txt
COMS 1111 AAAA
SMAS 2222 BBBB
MARC 3333 CCCC
MARCX 4444 DDDD
XCOMS 5555 EEEE

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. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

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

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

5. Shell Programming and Scripting

Replace values in a file with values from another file

Hi, I have 2 input files: File 1: echo Name > create_Name.txt echo Group /dir/group, Name >> create_Name.txt File 2: Name AAA BBB CCC group A B C dir A1 B1 C1 ................................ Need to replace the contents of File 1 with column 2, 3 & 4 values of File 2... (8 Replies)
Discussion started by: gctex
8 Replies

6. Shell Programming and Scripting

Referencing file for values and referencing another file to replace values

Hi I have a file which has values in each line: MP304,d40000 MP310,ff0000 etc I have another file which as the first value in it and is unique in the file(not repeated). I need to replace a string with the second value above. The second file contents is as follows:(snippet) <g ... (12 Replies)
Discussion started by: majikins
12 Replies

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

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. Shell Programming and Scripting

Replacing values in a file based on values in another file

Hi I have 2 files:- 1. List of files which consists of names of some output files. 2. A delimited file; delimted by "|" I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first... (5 Replies)
Discussion started by: pparthiv
5 Replies

10. Shell Programming and Scripting

PERL:How to convert numeric values txt file to PACKED DECIMAL File?

Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok (1 Reply)
Discussion started by: aloktiwary
1 Replies
Login or Register to Ask a Question