LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script question... (https://www.linuxquestions.org/questions/linux-newbie-8/script-question-704167/)

LinNewBie67 02-12-2009 08:36 AM

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.

indienick 02-12-2009 09:22 AM

Could you please post the code you are using to parse and print these values?

servat78 02-12-2009 06:30 PM

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

sycamorex 02-12-2009 06:42 PM

if you do it using awk, it would be:
Quote:

awk -F, '{ print $1 }' file
Here ',' is a field separator

fiomba 02-12-2009 07:44 PM

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

a='the quick, "brown", "fox", jumps over, "the lazy", "dog"'
lA=a.split(',')
# print tokens
print '\n'.join(lA)
# ___________ Output: _____________
# the quick
#  "brown"
#  "fox"
#  jumps over
#  "the lazy"
#  "dog"
print '========================'
# trim & remove "
for i in lA:
    print i.strip().strip('"')
# _________ cleaned output: ___________
# the quick
# brown
# fox
# jumps over
# the lazy
# dog

Run : python splitting.py (generally every Linux distro has python)

JulianTosh 02-13-2009 12:12 AM

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

LinNewBie67 02-14-2009 10:40 AM

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:

Originally Posted by Admiral Beotch (Post 3441866)
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



All times are GMT -5. The time now is 05:33 PM.