LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 04-16-2009, 05:04 PM   #1
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Rep: Reputation: 0
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
 
Old 04-16-2009, 05:20 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by will.flanagan View Post
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 View Post
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 View Post
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
 
Old 04-16-2009, 07:58 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by will.flanagan View Post
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
 
Old 04-17-2009, 10:40 AM   #4
will.flanagan
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks!

Thanks ghostdog74!,

Very helpful.

Cheers,
Will
 
Old 04-17-2009, 10:59 AM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM
awk questions? alaios Linux - General 4 05-13-2003 06:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration