LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-16-2009, 02:25 PM   #1
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Rep: Reputation: 0
manipulating ascii data tables questions


Hello linuxquestions friends,

I have 3 questions pertaining to manipulating tables of ascii data tables. I've spent a fair amount of time studying sort, awk, etc. in lowfatlinux and the man pages, but I'm still trying to figure out how to do a few things...

If I have a data.txt such as this:

1 " two words" blah 42
1.1 " three words here" blah 42

1. How can I delete every occurrence of " (for example) in a file?

2. Is there a way to treat the stuff in quotes as one entity in the awk command? for example, how could I change:

1 " two words" blah 42
1.1 " three words here" blah 42

to

1 42 blah " two words"
1.1 42 blah " three words here"

3. Is there a way to arrange the file onto a 'regular grid'. For example:

1 " two words" blah 42
1.1 " three words here" blah 42

to

1 " two words" blah 42
1.1 " three words here" blah 42

Sorry to ask three questions at once, but any response would be extremely helpful.

Cheers from La Serena!
Will
 
Old 04-16-2009, 02:41 PM   #2
maresmasb
Member
 
Registered: Apr 2009
Posts: 108

Rep: Reputation: 24
String manipulation of the kind that you are interested in, is more straightforward to do with some of the contemporary scripting languages, like PHP, Perl, Python, Ruby. You can of course do it in plain bash shell scripting by utilizing awk and sed and all the other tools, but it's so much easier to do in the mentioned other languages.

If you want to stick with shell scripting, then get some documentation with a lot of samples. String manipulation in bash or awk is awkward. Posting a complete tutorial would be way overkill.

Last edited by Tinkster; 10-30-2010 at 03:14 PM.
 
Old 04-16-2009, 03:20 PM   #3
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks for the suggestion maresmasb. I will look into Perl.

Nonetheless, if anyone can tell me how to do any of my three tasks, particularly numbers 1 or 2, that would be great!

Cheers,
Will
 
Old 04-16-2009, 03:30 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by will.flanagan View Post
Hello linuxquestions friends,

I have 3 questions pertaining to manipulating tables of ascii data tables. I've spent a fair amount of time studying sort, awk, etc. in lowfatlinux and the man pages, but I'm still trying to figure out how to do a few things...

If I have a data.txt such as this:

1 " two words" blah 42
1.1 " three words here" blah 42

1. How can I delete every occurrence of " (for example) in a file?
Code:
sed -i 's/"//g' file
with awk (much more verbose)
Code:
awk -F\" '{$1=$1;print $0}' file
Quote:
Originally Posted by will.flanagan View Post
2. Is there a way to treat the stuff in quotes as one entity in the awk command? for example, how could I change:

1 " two words" blah 42
1.1 " three words here" blah 42

to

1 42 blah " two words"
1.1 42 blah " three words here"
Code:
awk -F\" '{print $1 $3" \""$2"\""}' file
if you wanted to combine task 1 & 2 into 1:
Code:
awk -F\" '{print $1 $3 $2}' file
Quote:
Originally Posted by will.flanagan View Post
3. Is there a way to arrange the file onto a 'regular grid'. For example:

1 " two words" blah 42
1.1 " three words here" blah 42

to

1 " two words" blah 42
1.1 " three words here" blah 42

Sorry to ask three questions at once, but any response would be extremely helpful.

Cheers from La Serena!
Will
Looks the same to me?


Cheers,
Tink
 
Old 04-16-2009, 04:18 PM   #5
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks Tink!!! (one clarification...)

Thanks Tink - That's exactly what I was looking for!

'Looks the same to me?' - The format of what I wrote changed when posting question 3... my apologies.

Pretend that the 0's are also spaces, it should have read:

3. Is there a way to arrange the file onto a 'regular grid'. For example:

1 " two words" blah 42
1.1 " three words here" blah 42

to

100" two words"000000blah 42
1.1 " three words here" blah 42

I'm asking if there is a way to align the columns or 'put them on a regular grid'... Does that make sense now?

If anyone can answer this 3rd question, then my life will be complete.

Cheers,
Will
 
Old 04-16-2009, 04:53 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
OIC ... for future reference: if formatting is of the
essence, put things into code-tags [ code ] [ /code ]
(w/o the spaces between the [] and the words ...).
Code:
1 " two words" blah 42
1.1 " three words here" blah 42

to

1   " two words"        blah 42
1.1 " three words here" blah 42
And of course that can be done. The only difficulty
will be to determine the width (if they vary, and aren't
well-defined).

Code:
awk -F\" '{printf "%-4s%-20s%10s\n",$1,$2,$3}'


Cheers,
Tink
 
Old 04-17-2009, 10:44 AM   #7
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks! and are there any good tutorials?

Thanks Tink!

Also, are there any good tutorials on this sort of thing? Lowfatlinux introduced me to the awk command, but are there any tutorials that go into better detail with *lots of examples*?

The documentation on the man page is rather opaque to me... Cheers!

Will
 
Old 04-17-2009, 09:18 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I don't know of any good online tutorials other than
http://www.grymoire.com/Unix/Awk.html


There's the awk "book" (which should be part of your
distribution), and a few books (a very good one on
sed & awk by O'Reilly).


And truckloads of uses-cases for awk (including scripts
and discussions) here on LQ if you wanna use the search
a bit and have a hunt and peck.

And mailing lists on usenet, which are nicely searchable
via google.


Cheers,
Tink
 
Old 04-17-2009, 09:36 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can of course go to the manual
 
  


Reply



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
writing xml data into html tables kshkid Programming 2 05-22-2008 06:51 AM
MySQL/PHP get data from dissimilar tables jlinkels Programming 2 01-18-2008 12:31 PM
Selecting data from multiple mysql tables derzok Programming 3 10-14-2007 07:00 AM
LXer: Manipulating WPC Data through DAS with Eclipse LXer Syndicated Linux News 0 10-31-2006 07:33 AM
Dynamically select data from MySQL tables tangle Programming 6 02-28-2006 05:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:32 AM.

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
Open Source Consulting | Domain Registration