Using awk to when reading a file to search and output to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to when reading a file to search and output to file
# 1  
Old 02-15-2010
Using awk to when reading a file to search and output to file

Hi, I am not sure if this will work or not. I am getting a syntax error.

I am reading fileA, using an acct number field trying to see if it exists in fileB and output to new file. Can anyone tell me if what I am doing will work or should I attempt it another way? Thanks.
Code:
 
exec < "${fileA}
while read dataline
do
ACCT=`echo "${dataline}" | cut -c 1-7`
 
awk `{print substr($0,11,7)=="${ACCT}"}` "${fileB}" >> "${fileC}"

I am a syntax error on the awk line. Thanks in advance for your help.

Last edited by Scott; 02-15-2010 at 01:40 PM.. Reason: Please use code tags
# 2  
Old 02-15-2010
Code:
awk 'substr($0,11,7)== "'"${ACCT}"'" {print }' "${fileB}" >> "${fileC}"

# 3  
Old 02-15-2010
Thanks, I'll try it right now.

---------- Post updated at 12:52 PM ---------- Previous update was at 12:42 PM ----------

It didn't work but did get further. I have an eof unexpected issue but no file created.
# 4  
Old 02-15-2010
I think you need this:
Code:
awk 'NR==FNR{f[substr($0,1,7)];next}substr($0,11,7) in f{print $0>"file3"}' file1 file2


Last edited by Franklin52; 02-15-2010 at 02:22 PM.. Reason: Please use code tags!
# 5  
Old 02-15-2010
anbu23, Thanks so much. It worked perfectly. The problem was PEBCAC.

---------- Post updated at 01:12 PM ---------- Previous update was at 01:10 PM ----------

kcoder24. I just saw your response. Thanks also for responding. But it looks like I'm good now. You guys are great!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output path for file from search file

Hello Everyone, I am new to Unix and trying to learn as much as I can. But I do not need to create urgent scripts that I can use instantly. :(:( One of the scripts I am trying to write is essentially something that takes in a .txt file with product names. The product names are... (3 Replies)
Discussion started by: UN007
3 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

4. Shell Programming and Scripting

reading lines from a file between two search patterns

Hi, I am new to shell scripting and is working on a script to extract lines from a log file between two time stamps using awk command. After some research I used following command: awk '/01 Oct 2011/{p=1} /10 Oct 2011/{p=0} p' test.log >> tmp.log This works fine. But now i want to... (3 Replies)
Discussion started by: davidtd
3 Replies

5. Shell Programming and Scripting

Reading UNIX commands from file and redirecting output to a file

Hi All I have written the following script: #!/bin/ksh while read cmdline do echo `$cmdline` pid="$cmdline" done<commands.txt =========== commands.txt contains: ps -ef | grep abc | grep xyz |awk '{print $2}; My objective is to store the o/p of the command in a variable and do... (8 Replies)
Discussion started by: rahulparo
8 Replies

6. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

7. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies

8. Shell Programming and Scripting

Reading from a text file then /grep/output

I have a list of words that I want to grep in many files to see which ones have it and which ones dont. in the text file I have all the words listed line by line, ex: list.txt: check try this word1 word2 open space list .. I want to grep each line one by one. like I want it to... (7 Replies)
Discussion started by: s3rro
7 Replies

9. Shell Programming and Scripting

awk script to search an html file and output links

hello. i want to make an awk script to search an html file and output all the links (e.g .html, .htm, .jpg, .doc, .pdf, etc..) inside it. also, i want the links that will be output to be split into 3 groups (separated by an empty line), the first group with links to other webpages (.html .htm etc),... (8 Replies)
Discussion started by: kyris
8 Replies

10. Shell Programming and Scripting

Reading Input from File and Duplicates Output

Greetings to all, I would like to read input from a file and make duplications from it with Linux shell. For e.g. Input file ----------- ABC ABB ABA ------------------------------- Output file ------------ ABC ABC ABC ABB ABB (6 Replies)
Discussion started by: noelcantona
6 Replies
Login or Register to Ask a Question