How to refer both files in awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to refer both files in awk?
# 1  
Old 05-22-2013
How to refer both files in awk?

Hi,

In the script below, how can I refer both the input files at the same time?

nawk '{print NR "-" FNR "-" FILENAME}' f1 f2

output:
Code:
1-1-f1
2-2-f1
3-3-f1
4-4-f1
5-1-f2
6-2-f2
7-3-f2

I want output like:

Code:
1-1-f1 f2
2-2-f1 f2
3-3-f1 f2
4-4-f1 f2
5-1-f2 f1
6-2-f2 f1
7-3-f2 f1

regards,
juzz4fun
# 2  
Old 05-22-2013
You need to store f1 into an array, then print array and f2 at the same time.
Look at NR==FNR
If you post content of f1, f2 and the goal, we may help you
# 3  
Old 05-22-2013
I really don't understand what you are trying to achieve!

But you can define the file names in a variable inside BEGIN block and do something like below to get your desired output:
Code:
awk 'BEGIN{F1="f1";F2="f2"}{print NR "-" FNR "-" FILENAME, (FILENAME==F1?F2:F1)}' f1 f2

This User Gave Thanks to Yoda For This Post:
# 4  
Old 05-22-2013
The 2nd filename is unknown while the 1st file is processed!
You would need to pass it as another argument to awk.
# 5  
Old 05-22-2013
Quote:
Originally Posted by MadeInGermany
The 2nd filename is unknown while the 1st file is processed!
You would need to pass it as another argument to awk.
Not really.
The following should work:
Code:
nawk '{printf("%d-%d-%s %s\n",
        NF, FNR, ARGV[1 + (FNR!=NR)], ARGV[1 + (FNR==NR)])}' f1 f2

with a single pass through the files.
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python: Refer a properties file from different location

Hi All, I'm having a python script: test.py in /path/to/script/test.py I'm using a properties file: test_properties.py (it is having values as dictionary{}) which is in same DIR as the script. Sample Properties file: params = { 'target_db' : 'a1_db' 'src_db' : ... (15 Replies)
Discussion started by: saps19
15 Replies

2. UNIX for Beginners Questions & Answers

[[ -e $filename ]] - which man page to refer to these options

What does this do: f{ local logfile=$1 ] && logfile=$AMS_LOGFILE echo -e "--" } Can someone please guide me what does a) local variable do b) -z option stands for what? c) what other options are there (like -n, or -e, .., which man page do i refer to go through these... (6 Replies)
Discussion started by: AKS_Techie
6 Replies

3. Shell Programming and Scripting

[Solved] How to refer more than 9 command line inputs for a scripts in korn shell?

Hi all, I have a script which should take more than 9 command line inputs while running. Likescript.sh a s d f g h j j k l o p i u y t r e w Now in the script if I have to access one of the input which is at position after 9, in this case say 'p' then how can I do that? echo $12 will not work... (15 Replies)
Discussion started by: pat_pramod
15 Replies

4. UNIX for Dummies Questions & Answers

[Solved] How to refer to input file in code?

This may be a dumb question, but googling is not giving me an answer. I'm trying to figure out how to refer to an input file in my code. Lets say i run a script in bash: "sh shellscript.sh inputfile" (Inputfile will be variable...whatever file i run the script on) I wanted to make... (5 Replies)
Discussion started by: legato22
5 Replies

5. UNIX for Dummies Questions & Answers

Silly question-how i refer to the current directory when saving files

I want to save a bunch of files to a folder in my current directory. Ho do i refer to my current directory without writing all the path? (2 Replies)
Discussion started by: FelipeAd
2 Replies

6. UNIX for Dummies Questions & Answers

-0, what does it refer to?

When we write a Unix command for example like this: curl -0 ........ What is meant by "-0"? Thanks. (7 Replies)
Discussion started by: Abder-Rahman
7 Replies

7. Programming

How to refer to variable (korn shell)?

Hi I have the following block of code in korn shell and don't now how to refer to variable `print variable1.$dvd` ? --- integer dvd=4 integer number=0 while (( dvd!=0 )) do print "Iteracja numer : $dvd" print "$_" #it refers to $dvd var but want to refer... (3 Replies)
Discussion started by: presul
3 Replies

8. UNIX for Dummies Questions & Answers

Refer a remote file

I need to refer a remote(present on another unix server) directory from my unix machine as a local file. e.g. I have one directory D1 on 10.10.10.10 and i need to access files in this directory just like they are present on my unix machine 20.20.20.20. Is there any way out... i read a bit... (1 Reply)
Discussion started by: blackeyed
1 Replies

9. UNIX for Dummies Questions & Answers

I want to refer to an old command

I want to see a command that I typed a few days back. It looks like I can access only the latest 1000 commands through the history command. Is there a way I can access it or has it been deleted? (2 Replies)
Discussion started by: Legend986
2 Replies

10. Shell Programming and Scripting

how to refer one variable using another

Hi, How will you refer one variable's value using another in unix? For e.g Take the case System1_ip=172.120.20.54 And in latter part of the program while I'm using the values in a for loop I use i=System1 j=${i}_ip k=$j But K's value at the end of the run is System1_ip I want... (2 Replies)
Discussion started by: dayanandra
2 Replies
Login or Register to Ask a Question