Linux - NewbieThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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).
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.