array in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting array in awk
# 8  
Old 05-19-2011
Code:
awk 'NR==FNR{a[$0]; next} $6 in a{print > "/dir/to/" $6 "/newfile"}' /path/to/arrayfile file

It's unnecessary to use the script, the single awk command should do the whole job.
This User Gave Thanks to Franklin52 For This Post:
# 9  
Old 05-19-2011
Quote:
Originally Posted by Franklin52
Code:
awk 'NR==FNR{a[$0]; next} $6 in a{print > "/dir/to/" $6 "/newfile"}' /path/to/arrayfile file

It's unnecessary to use the script, the single awk command should do the whole job.
Maybe I am explaining it wrong. I have a text file that has multiple lines that are all formatted the same and the text is separated by a tab. Below is an example of this text file:
Code:
TEXT1A	TEXT2A	TEXT3A	TEXT4A	TEXT5A	TEXT6A	TEXT7A
TEXT1B	TEXT2B	TEXT3B	TEXT4B	TEXT5B	TEXT6B	TEXT7B
TEXT1C	TEXT2C	TEXT3C	TEXT4C	TEXT5C	TEXT6C	TEXT7C

I want to search each line in this file to see if the text field #6 matches the line I am searching for. I want to search from an array file one by one until done. See an example of the array file below:
Code:
TEXT6A
TEXT6B
TEXT6C

When a match is found, I want to print the entire line where that match was found to a file in a folder named after the text that I am searching for. For example, I want to write the whole line where the match occurred to /dir/to/TEXT6A/file.

I could not get the following to print the lines where the match occurred:

[QUOTE=Franklin52;302523738]
Code:
awk 'NR==FNR{a[$0]; next} $6 in a{print > "/dir/to/" $6 "/newfile"}' /path/to/arrayfile file

Please advise.
# 10  
Old 05-19-2011
how exactly does the solution provided by Franklin52 not solve the issue?
what exactly does not work?
# 11  
Old 05-20-2011
My mistake It works.

---------- Post updated at 09:56 PM ---------- Previous update was at 04:23 PM ----------

Upon further investigation I noticed that I still had the problem when working with real world data although all the tests worked fine.

The code below supplied by Franklin52 is correct although I need one modification
Code:
awk 'NR==FNR{a[$0]; next} $6 in a{print > "/dir/to/" $6 "/newfile"}' /path/to/arrayfile file

This code looks for the 6th field separated by spaces and tabs. When working with real world data, some fields contain spaces within the field and when these spaces exist this command doesn't work. I need to know how to modify the above command to look at the 6th field separated by tabs so it does not get confused when a space is introduced within a field.

Test sample file:
Code:
TEXT1A	TEXT2A	TEXT3A	TEXT4A
TEXT1B	TEXT2B	TEXT3B	TEXT4B
TEXT1C	TEXT2C	TEXT3C	TEXT4C

Real world file;
Code:
TEXT1A	TE XT2A	TEXT3A	TEXT4 A
TEXT1B	TE XT2B	TEXT3B	T EXT4B
TEXT1 C	TEXT2C	TEXT3C	TEXT4C

Thanks in advance.

---------- Post updated 05-20-11 at 12:24 AM ---------- Previous update was 05-19-11 at 09:56 PM ----------

I solved it. See below for the solution:
Code:
awk -F'[\t]' 'NR==FNR{a[$0]; next} $6 in a{print > "/dir/to/" $6 "/newfile"}' /path/to/arrayfile file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk array

Hello. I'm trying to figure out which one of these is not true about an awk array. -You do not need to formally declare an array; it is created automatically on first assignment -Array elements can only be addressed using an index ... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

4. Shell Programming and Scripting

Array from awk

I'm trying to get the output from awk into a bash array. Here is my script. #!/bin/bash while : do app=$( osascript -e "tell application \"System Events\" to return name of every process whose frontmost is true" ) echo "$app" if ava ]] then ps -ax | grep -v awk | pids=( $(awk... (5 Replies)
Discussion started by: nextyoyoma
5 Replies

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

array in awk

hi, can somebody explain me this? probably i am overlooking something but i dont know what why is not printed "7 9 11" instead of this? $ echo "" | awk '{for(i=1;i<=3;i++){j=7;a=j;j=j+2;} print a,a,a; }' 7 7 7thanks (2 Replies)
Discussion started by: erik80
2 Replies

7. Shell Programming and Scripting

AWK help. how to compare a variable with a data array in AWK?

Hi all, i have a data array as follows. array=ertfgj2345 array=456ttygkd . . . array=errdjt3235 so number or elements in the array can varies depending on how big the data input is. now i have a variable, and it is $1 (there are $2, $3 and so on, i am only interested in $1). ... (9 Replies)
Discussion started by: usustarr
9 Replies

8. Shell Programming and Scripting

Use of AWK as array

Dear Experts, I have a file containing text like below 123 456 789 012 345 I want to save each line in array. and print each array index as per my requirement. For Example array will print 456 array will print 012 Thanks for your Help. (14 Replies)
Discussion started by: Danish Shakil
14 Replies

9. Shell Programming and Scripting

array on awk

I have a raw data file: 70,1,1,-53.25 70,1,1,,,,-57.50 70,1,1,,,,,,,,,,-48.00 I want to have a output file with the formatting below: 70,1,1,-53.25,,,,-57.50,,,,,-48.00 I mean if these rows have the first similar three variables which will be group into one row. And I try to write a... (1 Reply)
Discussion started by: anhtt
1 Replies

10. Shell Programming and Scripting

Array in awk

Hi, How can find Array is NULL or not. i wrote script using arrays but i want dislpy an message if arrary was Empty. pls help me on this. (2 Replies)
Discussion started by: koti_rama
2 Replies
Login or Register to Ask a Question