LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   manipulating ascii data tables questions (http://www.linuxquestions.org/questions/linux-newbie-8/manipulating-ascii-data-tables-questions-719650/)

will.flanagan 04-16-2009 03:25 PM

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

maresmasb 04-16-2009 03:41 PM

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.

will.flanagan 04-16-2009 04:20 PM

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

Tinkster 04-16-2009 04:30 PM

Quote:

Originally Posted by will.flanagan (Post 3511348)
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 (Post 3511348)
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 (Post 3511348)
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

will.flanagan 04-16-2009 05:18 PM

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

Tinkster 04-16-2009 05:53 PM

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

will.flanagan 04-17-2009 11:44 AM

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

Tinkster 04-17-2009 10:18 PM

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

ghostdog74 04-17-2009 10:36 PM

you can of course go to the manual


All times are GMT -5. The time now is 08:21 PM.