Multiple pipes toward a single awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple pipes toward a single awk command
# 1  
Old 03-05-2009
Tools Multiple pipes toward a single awk command

Hello,

I would like to pipe two variables into awk, but I don't know how to do.

Each variable, "a" and "b", are in fact a list of data. They are not files.
So to get awk to work with it I am using:

Code:
 
echo $a | awk 'FNR==NR{print $1}FNR!=NR{print $4}'

The above works, but when I am trying with two inputs:

Code:
 
( echo $a; echo $b ) | awk 'FNR==NR{print $1}FNR!=NR{print $4}'

echo $a; echo $b are concatenated as a single input. Thus, my awk script will display $1 for both $a and $b.

Of course I could use:

Code:
 
echo $a > filea ; echo $b > fileb
awk 'FNR==NR{print $1}FNR!=NR{print $4}' filea fileb
\rm file[ab]

But that is very unconvenient.

Any suggestion?

Thanks

Edit: alternately, I am looking to solve the same problem, but using one variable and one file:
Code:
echo $a | awk 'FNR==NR{print $1}FNR!=NR{print $4}' fileb

but of course the above does not work!
# 2  
Old 03-05-2009
How about
Code:
echo $a $b

# 3  
Old 03-05-2009
Quote:
Originally Posted by Corona688
How about
Code:
echo $a $b

I already tried it and it does not give the expected result! Smilie
# 4  
Old 03-06-2009
use the -v option of awk to pass in variables
# 5  
Old 03-07-2009
Quote:
Originally Posted by ghostdog74
use the -v option of awk to pass in variables
Unfortunately I cannot. If I do that, I don't read any file with awk, so it won't run.

I know I could use:
echo "blah" | awk -v a=$a -v b=$b '{...}'

but in that case it does not do at all what I want.

For now my only solution is to "echo" my variable content in a temporary file and read from the file.

Thanks anyway.
# 6  
Old 03-07-2009
Place the code in BEGIN section, here's an example how to place the variables in arrays with the split function, supposing the lines are separated by a newline:

Code:
awk -v v1="$a -v v2="$b" 'BEGIN{
  n1=split(v1,arr1,"\n")    # n1 is the number of elements of the array
  n2=split(v2,arr2,"\n")
  print arr1[1]            # print the 1st element of arr1
}'

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sftp multiple files in single command

Hi All, I would like to sftp 2 files with a single command. I tried the below options, sftp suer@test13:"/u01/home/oracle/SetDb.sh /u01/home/oracle/.profile" ./ But what actually happens is Fetching /u01/home/oracle/SetDb.sh to /u01/home/oracle/.profile /u01/home/oracle/SetDb.sh ... (3 Replies)
Discussion started by: sid2013
3 Replies

2. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

3. Shell Programming and Scripting

running multiple command in a single line

Hi Can we run the linux command and per script in a single command $ cd /usr/local/adm/ ;ctsv scmtest_qabuild ;cspec.pl scmtest This is a combination of linux and clearcase command and last one is perl script with argument. I can see the first and 2nd coomand is executing but last... (6 Replies)
Discussion started by: anuragpgtgerman
6 Replies

4. Shell Programming and Scripting

Empty out multiple files with a single command?

I have a log directory: /logs/foo.log /logs/bar.log /logs/err.out I'm trying to find a way to > /logs/*.log > /logs/*.out to blank them out, but of course, that doesn't work. Any suggestions? (4 Replies)
Discussion started by: Validatorian
4 Replies

5. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

6. Shell Programming and Scripting

Multiples commands between pipes and a single process

Hi I have this script: #!/bin/ksh cmd1 | cmd 2 |cmd 3| cmd4 which it creates 4 process.... Is possible to create a single process PID1 which include all commands? Thanks Israel (2 Replies)
Discussion started by: iga3725
2 Replies

7. Shell Programming and Scripting

awk modify multiple columns with pipes

Hello, I have a CSV-like dataset where some of the columns contain HTML snippets which I need to convert to XHTML. For any given snippet, I have a functioning config for the text processor 'tidy' such that tidy -config tidy.cfg example.html does the job I need done. I would like to process... (10 Replies)
Discussion started by: bstamper
10 Replies

8. UNIX for Dummies Questions & Answers

How to run multiple command in single command?

Dear Unix Guru, I have several directories as below /home/user/ dir1 dir2 dir3 Each directory has different size. I want to print each directory size (Solaris command du -hs .) Can you please guide me how to achieve this? Thanks Bala (2 Replies)
Discussion started by: baluchen
2 Replies

9. Shell Programming and Scripting

Single column to multiple columns in awk

Hi - I'm new to the awk programming language. I'm trying to print a single column of data to several columns, and I found an article on iTWorld.com (ITworld.com - Printing in columns). It looks like the mkCols2 script is very close to what I need to do, but it looks like the end of the code... (2 Replies)
Discussion started by: astroDave
2 Replies

10. Shell Programming and Scripting

how to rename multiple files with a single command

Hi I have following list of files at a path: 01.AR.asset 01.AR.index 01.AR.asset.vf 01.AR.asset.xv I want to rename all these files as follows: 73.AR.asset.Z 73.AR.index.Z 73.AR.asset.vf.Z 73.AR.asset.xv.Z Can any body give me a single command to acheive the above results. ... (5 Replies)
Discussion started by: tayyabq8
5 Replies
Login or Register to Ask a Question