printing with awk through while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing with awk through while loop
# 1  
Old 08-11-2008
Question printing with awk through while loop

ive input file contains to clums a and b spreated by pipe
a | b
123|456
323|455

and
xyz contains other info about a and b

now i want to print as follows:

a | b | "info from xyz"

but "info from xyz" might be more than 1 line and i want to keep the format to 3 cloums.
how to do it?
i tried the belw script but printing was worng.



Quote:
while IFS='|' && read a b
do
c=`egrep ''$a'|'$b'' xyz.txt`
awk '{print $a":" $b":" $C}' > output.txt
echo "-----------" > output.txt
done < input.txt
# 2  
Old 08-11-2008
How do you propose to keep the input in three columns? Change the line endings inside $c to some other character, or repeat the fields from a and b for each line in $c?

Your awk script doesn't know about the shell's $c variable, they are not automatically communicated between the shell and awk so you need to do that somehow. However, the obvious solution fails to fix the problem with the multi-line output from $c.

Code:
awk -F '|' 'NR==FNR { c[a b] = 1; next; }
    { if c[$1 $2] { print } }' input.txt xyz.txt

# 3  
Old 08-11-2008
use sed to delete the new line chars from the c$ variable maybe? (i dont think i got your problem completly)
# 4  
Old 08-11-2008
Whatever which way you take, you should preformat your 2nd file with sed or awk so that everything that should be in a single row, is a single row. Then just use what era posted. If it is a simple pasting of files together, you can also use paste, but since you used egrep in your example, I guess "join" might be an option too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing the output of sed using a loop

So I am writing a bash script that will search a file line by line for unix timestamps, store all of the timestamps into an array, then check how many of those timestamps were created within the last hour, and finally increment a counter every time it finds a timestamp created within the last hour.... (6 Replies)
Discussion started by: jsikarin
6 Replies

2. Shell Programming and Scripting

Array not printing values if used in a loop

Hello! I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem. When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try... (3 Replies)
Discussion started by: arcoleman10
3 Replies

3. Shell Programming and Scripting

Awk: printing column using for loop

Hello: I've input data: Input data --- 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1 --- 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1 --- rs186476240:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1 I edit/make first few columns before numbers (6th column) and want to... (4 Replies)
Discussion started by: genome
4 Replies

4. 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

5. Shell Programming and Scripting

For Loop in shellscript - Printing Output for every iteration

for VGLIST in `lsvg -o` do CLOSED_OUT=`echo $VGLIST | lsvg -l $VGLIST | awk '{print $6 " " $7}' | grep closed` if ]; then echo "Filesystems $CLOSED_OUT in VG that are in Closed status" else echo "\n Some message" fi Above Code is working fine, but echo "Filesystems $CLOSED_OUT... (8 Replies)
Discussion started by: chandu123
8 Replies

6. Shell Programming and Scripting

Printing using awk

Hi I am relatively new to awk so i am getting confused a lot I am in need of help ... I am trying to append coloumns to the end of line using AWK I tried using this command awk -F "," '{for(s=7;s<=217;s++);$s="0";}1' OFS=, sam_sri_out It is giving me an output like this...... (1 Reply)
Discussion started by: Sri3001
1 Replies

7. Shell Programming and Scripting

Awk while-loop printing extra character

Hi, I'm using a while-loop in an awk script. If it matches a regular expression, it prints a line. Unfortunately, each line that is printed in this loop is followed by an extra character, "1". While-statement extracted from my script: getline temp; while (temp ~ /.* x .*/) print temp... (3 Replies)
Discussion started by: redbluefish
3 Replies

8. Shell Programming and Scripting

printing positional charaters in a loop

#!/bin/bash usep=`df -hT | awk '{ print $5 }'` for (1=1,1<8,i++) output=`echo $usep | awk '{ print $i }'| cut -d'%' -f1` echo $output if then echo "critical value" i need to echo critical value if disk usage pecentage xceeds 10 and i am face problem in position marked red here i... (9 Replies)
Discussion started by: josgeorge
9 Replies

9. UNIX for Dummies Questions & Answers

Silly question on printing for loop in AWK

Hi, One silly question. I would like to add statement like below and append to a file. I used the below code; however, it does not work. Can anyone please tell me what mistakes I have made? awk ' { for (i=1;i<=563;i++) print i }'>>output.txt Thanks. -Jason (1 Reply)
Discussion started by: ahjiefreak
1 Replies

10. Shell Programming and Scripting

Printing "END" before a new loop in AWK

First off, I have been learning AWK by trial and error over the last week or so, and there are some gaps in my basic understanding of the language. Here is my situation: I am coding and outputting results from an experiment I conducted in Psyscope, which has all been compiled into a master file.... (2 Replies)
Discussion started by: ccox85
2 Replies
Login or Register to Ask a Question