LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   awk questions (https://www.linuxquestions.org/questions/linux-newbie-8/awk-questions-719676/)

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

awk questions
 
Hi LinuxQuestions gurus,

I am trying to learn the awk command for data table manipulation. It seems like a very useful command and I am starting to understand it, but it is still rather opaque to me...

How can one make the field delimiter null so that each space is a field? Perhaps to change:

abcd

to

cbad

Also, are there easy ways to change all of the fields at once? An example might be adding commas:

blah blah blah blah ... blah blah

to

blah, blah, blah, blah, ... blah, blah

Last question: are there shorthand ways of only taking out one field? This would be easier than specifying each field in the print besides the on I'm taking out.
"awk '{print $1 $2 $4 $5 $6....}' example.txt"
(only missing $3 in order to remove it...)
An example might be:

abcde

to

abde

Sorry to bombard the forum with awk questions, but this is very helpful for me... Cheers!

Will

Tinkster 04-16-2009 05:20 PM

Quote:

Originally Posted by will.flanagan (Post 3511516)
Hi LinuxQuestions gurus,

I am trying to learn the awk command for data table manipulation. It seems like a very useful command and I am starting to understand it, but it is still rather opaque to me...

How can one make the field delimiter null so that each space is a field? Perhaps to change:

abcd

to

cbad

For that kind of thing you'd have to use the
FIELDWIDTHS variable .... BEGIN{FIELDWIDTHS="1 1 1 1 1 1 ...."

Quote:

Originally Posted by will.flanagan (Post 3511516)
Last question: are there shorthand ways of only taking out one field? This would be easier than specifying each field in the print besides the on I'm taking out.
"awk '{print $1 $2 $4 $5 $6....}' example.txt"
(only missing $3 in order to remove it...)
An example might be:

abcde

to

abde

Sorry, no, not that I'm aware of ... a bit of a kludge
and somewhat slower you could work with a loop.
Code:

for(i=1;i<=NF;i++){if(i!=3){printf $i};printf"\n"}
Quote:

Originally Posted by will.flanagan (Post 3511516)
Also, are there easy ways to change all of the fields at once? An example might be adding commas:

blah blah blah blah ... blah blah

to

blah, blah, blah, blah, ... blah, blah



Sorry to bombard the forum with awk questions, but this is very helpful for me... Cheers!

Will

OFS=", "

Code:

echo blah blah blah blah|awk 'BEGIN{OFS=", "}{$1=$1;print $0}'
blah, blah, blah, blah


Cheers,
Tink

ghostdog74 04-16-2009 07:58 PM

Quote:

Originally Posted by will.flanagan (Post 3511516)
How can one make the field delimiter null so that each space is a field? Perhaps to change:

abcd

to

cbad

set FS to "".
Code:

# echo abcd | awk 'BEGIN{FS=""}{print $3$2$1$4}'
cbad

Quote:

Also, are there easy ways to change all of the fields at once? An example might be adding commas:

blah blah blah blah ... blah blah

to

blah, blah, blah, blah, ... blah, blah
change OFS to ",".
Code:

# echo "blah blah blah blah ... blah blah" | awk '{$1=$1}1' OFS=","
blah,blah,blah,blah,...,blah,blah

Quote:

Last question: are there shorthand ways of only taking out one field? This would be easier than specifying each field in the print besides the on I'm taking out.
"awk '{print $1 $2 $4 $5 $6....}' example.txt"
(only missing $3 in order to remove it...)
An example might be:

abcde

to

abde
set which field to "". eg $1=$2="" means fields 1 and 2 set to "".
Code:

# echo "abcde" | awk 'BEGIN{FS=""}{$3=""}1' OFS=""
abde


will.flanagan 04-17-2009 10:40 AM

Thanks!
 
Thanks ghostdog74!,

Very helpful.

Cheers,
Will

PTrenholme 04-17-2009 10:59 AM

If you're using gawk (to which awk is often symlinked), see if you have the gawk info file installed. It's quite helpful, and includes many worked out examples.


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