IF awk in a while read line-loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting IF awk in a while read line-loop
# 8  
Old 05-18-2012
If both files are sorted, and there are no 'missing records' in the first file (there can be, as you show, missing records in file2), then this might work. It does what you want on the small sample you've pasted.

Code:
awk  '
    {
        t1 = $1;
        printf( "%s ", $0 );
        if( !t2 )
        {
            if( (getline < f2) <= 0 )
                eof_f2 = 1;
            else
            {
                t2 = $1;
                $1 = "";
                r2 = $0;
            }
        }

        if( !eof_f2 )
        {
            if( t2 == t1 )
            {
                printf( "%s", r2 );
                t2 = r2 = "";
            }
        }
        printf( "\n" );
    }
'  f2=file2  file1>output-file

This User Gave Thanks to agama For This Post:
# 9  
Old 05-18-2012
How about this ?

Code:
awk 'NR==FNR{a=$1;$1="";array[a]=$0;next}
{if(array[$1]){print $0,array[$1]}else{print $0}}' file2 file1

~ Pravin ~
This User Gave Thanks to pravin27 For This Post:
# 10  
Old 05-18-2012
This should work on your original data (where file1 has extra :xx seconds on the timestamp):

Code:
awk '
    {
        t1 = $1;
        printf( "%s ", $0 );
        if( !t2 )
        {
            if( (getline < f1) <= 0 )
                eof_f2 = 1;
            else
            {
                t2 = $1;
                gsub( ":[0-9][0-9]$", "", t2 );
                $1 = "";
                r2 = $0;
            }
        }

        if( !eof_f2 )
        {
            if( t2 == t1 )
            {
                printf( "%s", r2 );
                t2 = r2 = "";
            }
        }
        printf( "\n" );
    }
' f1=file1 file2 >output-file

This User Gave Thanks to agama For This Post:
# 11  
Old 05-19-2012
Try:
Code:
awk 'NR==FNR{n=$1; $1=x; A[substr(n,1,14)]=$0; next} {print $0 A[$1]}' file1 file2


Last edited by Scrutinizer; 05-19-2012 at 02:47 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 05-19-2012
Hi!

i'm verry grateful to all of you for your help.
All the code is working fine, but the fildseperator in the outputfile
is not TAB, as it should be. I tried it by my own, but unfortunately
not successful. Can you please give me one more hint.

Thanks in advance!
IMPe

Code:
#!/bin/bash -e
awk  '
    {
        t1 = $1;
        printf( "%s\t", $0 );
        if( !t2 )
        {
            if( (getline < f2) <= 0 )
                eof_f2 = 1;
            else
            {
                t2 = $1;
                $1 = "";
                r2 = $0;
            }
        }

        if( !eof_f2 )
        {
            if( t2 == t1 )
            {
                printf( "%s\t", r2 );
                t2 = r2 = "";
            }
        }
        printf( "\n" );
    }
'  f2=pd.txt mete.txt > pd_gnuplt.txt

# 13  
Old 05-19-2012
Try:
Code:
awk 'NR==FNR{n=$1; $1=x; A[substr(n,1,14)]=$0; next} {$0=$0 A[$1]}$1=$1' OFS='\t' file1 file2

This User Gave Thanks to Scrutinizer For This Post:
# 14  
Old 05-19-2012
MySQL

Hi!
Thank you - now i put all together and it is exactly doing what i want.
really appreceating your patience and help
thank you! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While read line loop

Based on text file: PATH:/media/hdd/Media/Video/title1 FILE:/media/cache/281662-14.jpg PATH:/media/hdd/Media/Video/title2 FILE:/media/cache/281662-15.jpg PATH:/media/hdd/Media/Video/title3 FILE:/media/cache/281662-16.jpg PATH:/media/hdd/Media/Video/title4 FILE:/media/cache/281662-17.jpg... (12 Replies)
Discussion started by: TiedCone
12 Replies

2. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

3. Shell Programming and Scripting

Simple while read line loop question

#!/bin/bash count=1 while read line do if (($count > 4)); then awk -v var1="$count" '{printf "%3s%8s%11s%11s%11s\n",var1,$2,$3,$4,$5}' else echo $line fi count=$((count+1)) done < posre_sub.itp > test cat test INPUT: ; position restraints for... (3 Replies)
Discussion started by: origamisven
3 Replies

4. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

5. Shell Programming and Scripting

While loop read line not working

Hi, I am trying to read a file line by line inside of a while loop. This while loop is part of a here document. while read line do ssh -t $2@$remotehost <<REMOTE ls path/to/dir > $path_to_dir while read line1 do echo "LINE --- $line" done... (4 Replies)
Discussion started by: mnanavati
4 Replies

6. Shell Programming and Scripting

While loop read line Issue

Hi I am using while loop, below, to read lines from a very large file, around 400,000 rows. The script works fine until around line 300k but then starts giving incorrect result. I have tried running the script with a smaller data set and it works fine. I made sure to include the line where... (2 Replies)
Discussion started by: saurabhkumar198
2 Replies

7. Shell Programming and Scripting

While read line loop

Hi I'm writing a bash script which will read an input file and look for occurrences of the current user ($USER) executing the script. When i find the occurrence of the username I take that line and append it to a file with a line number and bracket display next to line. The input file has been... (12 Replies)
Discussion started by: BundBash
12 Replies

8. Shell Programming and Scripting

read line in a for loop

Hi All, How can we use read line using the index value of a FOR loop? eg: pt_mstr,pt_status,8 pt_mstr,pt_buyer,8 pt_mstr,pt_sfty_stk,8 pt_mstr,pt_ord_pol,3 pt_mstr,pt_capacity,8 pt_mstr,pt_plan_ord,3 pt_mstr,pt_ord_mult,8 From this file i want to read the line2, 3 and 4 only using a FOR... (3 Replies)
Discussion started by: balajim
3 Replies

9. UNIX for Dummies Questions & Answers

Alternative for a while read line loop

HELLO all :), I have been trying to use a simple while loop to read a file " templist", line by line and perform an action. See the code below. The reason for not using a while read line loop is the for the use of the if condition that wouldn't work. I would appreciate some ideas as this has... (2 Replies)
Discussion started by: kabs
2 Replies

10. Shell Programming and Scripting

Nested while read line loop

Hi, Can anyone please help me: i'm trying to read a file with directory-names , then go to that directory and read another (output) file to perform some tasks per line (second read line in the part of script below). The problem is that after the nested while loop has finished, the first while... (7 Replies)
Discussion started by: Rakker
7 Replies
Login or Register to Ask a Question