Multiple file input

Once a time, I did see a way to multiply a file input in bash, but have forgotten how it was done.

Eks.

awk 'FNR==NR {a[$1]++;next} ($2 in a) {print $2}' file file

Here you need file two times.
I seen it some like this

awk 'FNR==NR {a[$1]++;next} ($2 in a) {print $2}' 2&file

You seem to have already answered your question, what would you like from us? That is awk, not bash, by the way.

I was just looking into this. What I've found is the following:

awk 'NR==FNR{a[$1];next}$1 in a{print $1}' F1 F2

NR==FNR -- this will only be true when in the first file
a[$1] use the first column to make array of file
next skip all the next blocks and process the next line
($1 in a) check in value in column on of second file
then print it

gawk 'FNR==NR {a[$1]++;next} ($2 in a) {print $2}'

So this is NR==FNR same as above
a[$1] s same as above
$2 in a -- is column $2 in the array of second file? then print it

@Corona688
This does not work, it gives me:

awk 'FNR==NR {a[$1]++;next} ($2 in a) {print $2}' 2&file
[1] 7183
awk: cmd. line:1: fatal: cannot open file `2' for reading (No such file or directory)
t: command not found
[1]+  Exit 2                  awk 'FNR==NR {a[$1]++;next} ($2 in a) {print $2}' 2

2&file would be some like this. Run awk with file name 2 , and if ok, the run command file
I would like to substitute file file with some that returns file two or more in on go.

@newbie2010
My awk does compare first and second column, and if equal print what is equal.
It does not care what row its in, just compare every number anywhere in the column.

-- deleted--

Well, it means what it says... There's no file named '2'. If you want to give it some other, more valid filename, you are free to do so.

It takes the & to mean 'run this command in background and take the text after as a new command'.

I do not understand this. If you want awk to read the file twice, you tell it the file twice; that's how it works. But I don't think your question is crossing the language barrier very well.

Do you have a couple files with output? Files before you run the command, and what you expect after?

Also, I've noticed that on my system a lot of what runs for others with awk only works with gawk for me. Have you tried gawk?

I just what to use the same file multiple times and write it once.

Example.

awk 'code' file file file file

This awk reads file 4 times and do some with the data.
I would like

awk 'code' 4xfile

Write name of file just once, and tell it should be done four times.

You may try something like this

$ cat test
1
2
3
4
5
6
7
$ awk 'FNR==NR{print $0;next}{print "yes"$0}' $(seq -s "test " 3 | tr -d '[:digit:]')
1
2
3
4
5
6
7
yes1
yes2
yes3
yes4
yes5
yes6
yes7
$ awk 'FNR==NR{print $0;next}{print "yes"$0}' $(printf 'test %.0s' {1..2})
1
2
3
4
5
6
7
yes1
yes2
yes3
yes4
yes5
yes6
yes7

Ah. Well, awk doesn't work that way, you'll just have to write a loop.

FILENAME="a"
LOOPS=4
while [ $loops -gt 0 ]
do
        FILES="$FILES $FILENAME"
        let LOOPS=LOOPS-1
done

awk '{...}' $FILES

Ok. I have seen it used, so I have to search.

Seen what used? awk really doesn't work that way...

You may have seen an obscure feature of some specific shell, to repeat a string a certain number of times.

Found it after reading some hundreds of post {,} :

awk 'FNR==NR {a[$1]++;next} ($2 in a) {print $2}' file file

is the same as

awk 'FNR==NR {a[$1]++;next} ($2 in a) {print $2}' file{,}
awk 'code' file file file file

is the same as

awk 'code' file{,,,}

Is this obscure?

It's not an awk feature, try this:

$ echo file{,,,}
file file file file

$

Yes, it's pretty obscure, and doesn't work in all Bourne shells. Unfortunately it isn't what you asked for either, you wanted to give it a number.

This was what I ask for. How to shorten file file to some shorter.
An file{,} is the answer.

Glad you found what you were looking for, then.

I am to :slight_smile:
After finding it out I have examined it some and found out that its not obscure at all.
It just standard brace expansion

Just look at

echo 1{a,b}
1a 1b

so when removing a and b , it will add noting for both side of the comma

echo 1{,}
1 1