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 |
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. |
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 |
Quote:
Code:
sed -i 's/"//g' file Code:
awk -F\" '{$1=$1;print $0}' file Quote:
Code:
awk -F\" '{print $1 $3" \""$2"\""}' file Code:
awk -F\" '{print $1 $3 $2}' file Quote:
Cheers, Tink |
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 |
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 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 |
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 |
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 |
you can of course go to the manual
|
All times are GMT -5. The time now is 10:30 AM. |