little problem of file redirection (awk)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers little problem of file redirection (awk)
# 1  
Old 09-27-2012
little problem of file redirection (awk)

I almost reach my objective (Youhouuu !!!!)
But I really don't understand why it doesn't work until the end... Smilie

For clarity's sake I am taking a very simple example. The operations I am doing in the script (gsub and print) really don't have any importance !!! I just matter about redirecting the file that I produced inside the script.


input file called "original.txt":
Code:
book # 001|line 1|America
book # 002|line 56|Asia
book # 003|line 38|Europe

I remove the expression "line " in $2 and would like to redirect the output file called "for_later.txt" because I will need to have a trace of the data I produced at this stage and I will use them later for something else (something even totally different from programming).

But for now, I need to keep processing the original input file "original.txt" by adding another field ("Price"), like if I've never redirected anything.

Here what I tried:
Code:
BEGIN{FS=OFS="|"}

{ 
    sub("line ","",$2)
    print $0 > "for_later.txt"   # at this stage of the script I redirect the output that I will need later
}

{
    while((getline < "for_later.txt") > 0)   # here I redirect the produced file to keep processing it
    print $0 FS "Price:   $"
}

Problem:
The file "for_later.txt" looks perfectly fine:
Code:
book # 001|1|America
book # 002|56|Asia
book # 003|38|Europe

However my final output produced when awk finish executing the entire script only display the first line (the content of this line is correct though):
Code:
book # 001|1|America|Price:   $

Question: Why the 2 other lines are missing ???
the final output I would like for now:
Code:
book # 001|1|America|Price:   $
book # 002|56|Asia|Price:   $
book # 003|38|Europe|Price:   $

* I also tried every kind of getline:
getline
getline var
getline < file
getline var < file
command | getline (with the shell command "cat for_later.txt")
command | getline
command |& getline
command |& getline var

* I tried to close the file before calling getline.

* I tried "if" instead of "while", and even with no statements.

It always return the first line only !

Please, if someone could explain what is happening it would be great.
Thanks !

Last edited by beca123456; 09-28-2012 at 12:39 AM..
# 2  
Old 09-27-2012
Your description of the problem does not mention anything about for_later.tab but you use it in your script. What does it look like?
# 3  
Old 09-28-2012
Whoops sorry, in the code I meant "for_later.txt" (I corrected the code). There is no .tab file
Quote:
The file "for_later.txt" looks perfectly fine:

Code:
book # 001|1|America
book # 002|56|Asia
book # 003|38|Europe
# 4  
Old 09-28-2012
Actually when I tried your code in gawk, I did not get any terminal output. That may be because gawk buffers its output. So, the file for_later.txt actually has records only when the program finishes (or when the buffer is flushed) and not in between.

But, I could simulate the behaviour mentioned by you by putting in a system call with a null string (system("")) after print $0 > "for_later.txt". This flushes the output buffers to their destinations.

When I tried your code with the awk which comes with AIX 6.1, I got what you want to see:
Code:
book # 001|1|America|Price:   $
book # 002|56|Asia|Price:   $
book # 003|38|Europe|Price:   $


Last edited by elixir_sinari; 09-28-2012 at 04:12 AM..
# 5  
Old 09-28-2012
I am using gawk as well and I get only the first line on the terminal output.

Code:
2) the while loop tries to read a line at a time from for_later.txt until an error. This reads only the first line (output by 1) and prints it.

So, is there any way to "force" or bypass the error exit status of getline and set it to 1 in order to keep reading the remaining line of "for_later.txt"?
# 6  
Old 09-28-2012
Why don't you modify your script to this?:
Code:
BEGIN{FS=OFS="|"}

{ 
    t=$0
    sub("line ","",$2)
    print $0 > "for_later.txt"   # at this stage of the script I redirect the output that I will need later
    print $0,"Price:   $"  
}

# 7  
Old 09-28-2012
As I mentioned in my first post, this is a simple example to show the problem of redirecting the entire file into the script.

The purpose of this thread is to know how I could do that with getline or other strategies.

But thanks anyway !

So I presume it is not possible.

Last edited by beca123456; 09-28-2012 at 03:34 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk file redirection issue

So I'm writing a script which tries to parse human-readable addresses. Part of it is this: print $2, implode(A,1,AN," "), CITY, PROV, POST, COUNTRY, CITYCOUNT>2; CITYCOUNT is a variable between 0 and 3 counting the number of words in a city name. I'm trying to prnt 1 wherever that's greater... (5 Replies)
Discussion started by: Corona688
5 Replies

2. Shell Programming and Scripting

Problem with file redirection in while

I've got a problem with file redirection in bash, in loop while (in my script done < bufor.txt). In every consecutive iteration there's a possibility that to the bufor.txt will be added some lines. Unfortunately, in loop, added lines are ignored (for example: bufor.txt has 5 lines, in 3rd iteration... (4 Replies)
Discussion started by: kk9
4 Replies

3. Shell Programming and Scripting

Problem of redirection with <<! ... !)

Hi, I have to migrate Korn Shell who call Sybase command into Oracle Command. I have a problem on this example : DATNUM=$( sqlplus -S SA/SASASA@MIGGVDEV <<! SET FEEDBACK OFF; SET HEADING OFF; select sybase_utilities.datediff('DAY', '01/01/1900', '01/01/2000') from... (4 Replies)
Discussion started by: buzzy1804
4 Replies

4. Shell Programming and Scripting

awk output redirection to file

I have a system stat command running which generates data after 5 sec or so. I pass this data to awk and do some calculation to present the data differently. Once done now I want to pass this data to file as and when generated but doesn't work..unless the first command completes successfully.... (6 Replies)
Discussion started by: learnscript
6 Replies

5. Shell Programming and Scripting

awk print redirection to variable file name

Hello, i need to redirect the output of print to a variable file name: #This is normal awk '{ print $17 > "output.txt" }' input #I need something like this awk '{ print $17 > "output_${25}.txt" }' input how to format the output file name to contain a variable? (6 Replies)
Discussion started by: nazeeb
6 Replies

6. Shell Programming and Scripting

Redirection problem

hi i want a solution for the fallowing: i am redirecting output from bash script in which we are running background commands like ./test.sh contains ls -l & ps when we do as ./test.sh 1> test.txt 2>&1 but test.txt not containing output as ls -l and ps. it showing ls -l, ps and... (2 Replies)
Discussion started by: indianwomen
2 Replies

7. Shell Programming and Scripting

file redirection problem

my querry is suppose i have duplicate std i/p with FD-3 --exec 0<&3 now redirected std i/p to a file ----exec 0<file1 suppose i am reading the file line by line --while read LINE cutting some fields and comparing it with a variable and if a match is found ... (0 Replies)
Discussion started by: mobydick
0 Replies

8. Shell Programming and Scripting

awk two file redirection

Hi, i use awk -F to print three variable delimited by comma $1 $2 $3 if $2=="" i want to extract this information missing from another file using awk -v + some process. but the problem i can't use the two awk together cause of redirection there's a solution. note: i can't use another... (1 Reply)
Discussion started by: kamel.seg
1 Replies

9. Shell Programming and Scripting

redirection problem

Hello Experts, I am facing the following problem .. >cat my_file prints initial begin StartSim; LDR(on); // Lock Detect Enable But when I trying the same with the following command it shows me all the line in the same line.Like ... (4 Replies)
Discussion started by: user_prady
4 Replies

10. UNIX for Dummies Questions & Answers

Redirection Problem

I was simply trying to redirect some output in a log file but it foxed me. truss a.out > log It gave only the output of the a.out which was only a simple print "Hello" Then I tried truss a.out 2&1>a echo `truss a.out` > a All failed. What am i missing. Thanks (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies
Login or Register to Ask a Question