for loop problem
I am having trouble with a "do for" loop that is supposed to read a file line by line and then give structured output. When I run it I get multiple copies of the output rather than one. Any help would be appreciated.
My script for i in `cat "$filename"` do awk -F: '{print $1, $2, $3}' "$filename" done Like I said it works but gives me multiple copies of the output. I actually set IFS=":" just before this as a colon is the field separator in the files I need to process. My question is the "for i in" correct to read the file one line at a time |
When posting code examples, wrap them in the CODE tag.
Your code contains at least two errors. There's no closing quote that matches the ' before cat. There's no { to match the } in the awk command. Did you copy/paste this code or re-type it by hand? (Or to put it another way, is this actually the exact code you're using?) What you've posted will not read one line at a time if there is any whitespace in any of the lines in the file. E.g. this Code:
one Can you post a sample of the input file and the output you're getting. |
Please ss the updated original post. I only posted the part dealing with the for loop. When I sad multiple lines I mean I get all the files line multiple times. For instance the first file has 15 lines and I get each 4 times
|
As I said, the code you've posted it has obvious errors in it. If that is the actual code you're using, fix the errors I mentioned and try again. If that is not the code you're using then post the code you are using.
Also post examples of the input you're using and the output you're getting. It might also help if you posted an example of the output you are trying to get. You're setting i to each 'line' but you're not using the $i variable inside the loop. So if you're trying to run an operation of each 'line', you're not doing that. |
You are invoking awk as many times as you have words in the file (for i in `cat $filename`;...). And then each time awk traverses the file and prints out your first three fields of each line of the file. Here is the simple example to validate this.
Code:
$ cat $filename Code:
$ wc -lw $filename | awk '{print $1 * $2}' |
Ultimately the question boils down to what is it you are trying to do?
Either bash OR awk can do what you seem to be trying on their own so currently the script seems to not make any sense. konstan has provided the why as far as what you are seeing occur, but to help with the how of what you want to do, you will need to explain that first. |
Code:
while IFS=":" read -r a b c d |
I guess the question is
I guess my question is if the "i" reads the number of words what do I use to use the number of lines in the file. Like I originaly said the script works as written except it executes to many times. I understand this probably isn't the "advanced" way to do this, that is why I posted it in the NEWBIE section.
What I am trying to do is exacly as I originally stated, out put the columns from the file as it reads them. I entered the script here by hand as every time I try to copy and paste it I end up with a copy in the script and not here. |
I think it would be simpler if you showed us a few lines of the input file, and the expected output for those lines (all in code tags of course).
|
Quote:
Quote:
Quote:
What is happening is that 'i' is assigned a value from the file based on the IFS variable value for word splitting (normally white space). As others have indicated, had you included an example of the input it would help to visualize what you are trying to do. You are setting the IFS value to a colon so we are guessing this means the file is delimited as such. So if we assume the following input file format: Code:
a:b:c As your example then uses awk to read from the same file, it will now read the entire file and display as you have requested. It will perform this task 6 times, ie. the entire file processed by awk 6 times. So this leads me back to my response in post #6, either use bash or use awk. Examples: Code:
#awk (interestingly you already have your solution) |
Use $(command) instead of backticks (`command`).
It's easier to read, connot be confused with quotes, and nests easily. |
Thanks for all the help!
|
All times are GMT -5. The time now is 09:52 PM. |