LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Reading files in shell scripting (https://www.linuxquestions.org/questions/linux-software-2/reading-files-in-shell-scripting-840295/)

GrapefruiTgirl 10-25-2010 10:48 AM

You are not paying attention to details! You did not set IFS in your second codeblock above. Watch:

Code:

root@reactor: awk -F":" '{OFS=":"; print $1, $2, $3, $4, $5}' TestFile
1::2:3:4
4:5:6:7:8
7:8:9:10:
7: 8 9:1:2:3
root@reactor: IFS=":"; awk -F":" '{OFS=":";print $1,$2,$3,$4,$5}' TestFile | while read one two three four five; do echo "two : $two"; done
two :
two : 5
two : 8
two :  8 9
root@reactor:

Now, output appears, to me anyway, to be correct. Is it not?

barunparichha 10-26-2010 01:41 AM

Code:

IFS=":"
awk -F":" '{OFS=":";for(x=1;x<=5;x++){if($x==" "){$x="*"}};print $1,$2,$3,$4,$5}' TrackMsgFile.0806 | while read one two three four five; do
 echo "Here's your vars: '$one' '$two' '$three' '$four' '$five'"
done

solves the issue raised in post #11.
But, could not solve the empty space issue addressed in post #8.

Thanks.

GrapefruiTgirl 10-26-2010 05:18 AM

OK, I thought the empty space issue was addressed by returning the "*" character when a field contains only an empty space. I did however adjust the code in post #10 so that if either a space or an empty field (between two colons) is detected, a "*" is returned.
If this doesn't address any remaining issue, then I'm afraid I'm missing something about this situation. If none of these ideas work, and the ideas presented by the other members in this thread, are of no help either, than it may be a good idea to demonstrate anew the requirements of this problem:

The way I understand it is: you have an input file containing 5 fields, delimetered by colons. Fields might be empty or contain a space (meaning it is empty), but the colon delimiters are always there. You want each of the 5 fields' values returned into a shell variable for processing each time a line from the file is read in. If a space is contained within one of the fields, it doesn't matter, right? I mean, if a field contains "John Smith", would you prefer to have returned "JohnSmith" or "John*Smith"?

So, I thought the requirements have been met. If there's something I've not got right in that, please correct me, and:

--Show us the exact input file.

The part about assigning the fields to the variables, is easy enough; the problem here seems to have to do with spaces, but I don't understand just what that problem is.

Thank you for you patience!

EDIT: Plus, if there's someone else reading this, who sees what I'm missing, by all means, jump in!


All times are GMT -5. The time now is 08:16 PM.