Script question...
Hi there,
I have a question about a problem I am encountering while writing something in bash. This is the situation: I have a file in which the data is separated as follows. example file: ************************************************** field1, "field2", "field3" the quick, "brown", "fox" jumps over, "the lazy", "dog" ************************************************** I am able to display the second and third column however if I want to display the content of the first field I am able to only display the last word of this field e.g. of output. quick, brown, fox over, the lazy, dog If it is not quotes I cannot display it properly. Any clues, suggestions on how to go about it? Appreciate, Newbie in Linux. |
Could you please post the code you are using to parse and print these values?
|
Without the quotes your contents for the first field will become several fields due to the whitespaces. Your code needs to be changed to handle such issues. One possibility is to split by comma's instead of whitespace. This whole problem entirely depend on your scripting code.
Debian |
if you do it using awk, it would be:
Quote:
|
I always solve such problems (especially if you have to split)
with Python and not Bash. In such a way the solution is straightforward: Code:
# splitting.py |
Given your input text above, here's a little script to fix the malformed cvs data...
# Cut columns into their own files cut -f1 -d, abc.txt > abc.f1 cut -f2 -d, abc.txt > abc.f2 cut -f3 -d, abc.txt > abc.f3 # Standardize quotes in each row of column files sed 's/\"//g; s/\(.*\)/\"\1\"/' abc.f1 > abc.ff1 sed 's/\"//g; s/\(.*\)/\"\1\"/' abc.f2 > abc.ff2 sed 's/\"//g; s/\(.*\)/\"\1\"/' abc.f3 > abc.ff3 # Paste it all together paste -d, abc.ff1 abc.ff2 abc.ff3 |
Thank you...
Thank you,
you gave me a good pointer. At the end I redirect everything to a new file e.g. paste -d, abc.ff1 abc.ff2 abc.ff3>tmpfile.tmp from there it is easy to read this tmpfile Thanks again, Newbie with Linux Quote:
|
All times are GMT -5. The time now is 05:33 PM. |