LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-12-2009, 09:36 AM   #1
LinNewBie67
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Rep: Reputation: 0
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.
 
Old 02-12-2009, 10:22 AM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Could you please post the code you are using to parse and print these values?
 
Old 02-12-2009, 07:30 PM   #3
servat78
Member
 
Registered: Jan 2009
Posts: 100

Rep: Reputation: 17
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

Last edited by servat78; 02-19-2009 at 12:21 PM.
 
Old 02-12-2009, 07:42 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200Reputation: 1200
if you do it using awk, it would be:
Quote:
awk -F, '{ print $1 }' file
Here ',' is a field separator
 
Old 02-12-2009, 08:44 PM   #5
fiomba
Member
 
Registered: Sep 2004
Posts: 63

Rep: Reputation: 15
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)
 
Old 02-13-2009, 01:12 AM   #6
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
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
 
Old 02-14-2009, 11:40 AM   #7
LinNewBie67
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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 View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script question pvpnguyen Programming 4 03-03-2008 10:20 AM
script question paul_mat Linux - Networking 1 04-18-2006 01:01 AM
script question lordofring Linux - Software 2 10-27-2005 01:15 PM
Question on this script. Echo Kilo Programming 2 12-13-2004 05:20 AM
script question shanenin Linux - Software 2 04-04-2004 11:39 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration