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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-16-2009, 02:25 PM
|
#1
|
|
LQ Newbie
Registered: Apr 2009
Posts: 15
Rep:
|
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
|
|
|
|
04-16-2009, 02:41 PM
|
#2
|
|
Member
Registered: Apr 2009
Posts: 108
Rep:
|
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.
|
|
|
|
04-16-2009, 03:20 PM
|
#3
|
|
LQ Newbie
Registered: Apr 2009
Posts: 15
Original Poster
Rep:
|
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
|
|
|
|
04-16-2009, 03:30 PM
|
#4
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,916
|
Quote:
Originally Posted by will.flanagan
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
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
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
|
|
|
|
04-16-2009, 04:18 PM
|
#5
|
|
LQ Newbie
Registered: Apr 2009
Posts: 15
Original Poster
Rep:
|
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
|
|
|
|
04-16-2009, 04:53 PM
|
#6
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,916
|
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
|
|
|
|
04-17-2009, 10:44 AM
|
#7
|
|
LQ Newbie
Registered: Apr 2009
Posts: 15
Original Poster
Rep:
|
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
|
|
|
|
04-17-2009, 09:18 PM
|
#8
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,916
|
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
|
|
|
|
04-17-2009, 09:36 PM
|
#9
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
you can of course go to the manual
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:55 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|