Remove a field using AWK | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Remove a field using AWK

Shell Programming and Scripting


Tags
remove awk, remove fields awk

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-02-2010
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
Remove a field using AWK

Need to remove one of the field and print the remaining fields using awk. For ex

Quote:
MRT_DM MR_DI_AD ST_DJ JUN_SAL
if i have the above line as input, need to remove either one of the column, say ST_DJ and the output should be as below using AWK command. Im able to do this with sed but not sure with AWK. Pls throw some light
Quote:
MRT_DM MR_DI_AD JUN_SAL
Sponsored Links
    #2  
Old 09-02-2010
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 24 October 2014, 1:54 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,237
Thanks: 121
Thanked 453 Times in 412 Posts
Deleting the 3rd field, no matter what it is:

Code:
$> awk '{$3=""; print}' infile
MRT_DM MR_DI_AD  JUN_SAL

Substituting the given string with nothing:

Code:
$> awk '{sub(/ST_DJ /,""); print}' infile
MRT_DM MR_DI_AD JUN_SAL

Sponsored Links
    #3  
Old 09-02-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 October 2014, 7:38 AM EDT
Location: The Netherlands
Posts: 7,719
Thanks: 123
Thanked 538 Times in 508 Posts
To delete a specific column you can do something like:

Code:
awk '{$3="";sub("  "," ")}1' file

    #4  
Old 09-02-2010
michaelrozar17's Avatar
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 17 March 2014, 1:43 AM EDT
Location: IN
Posts: 892
Thanks: 14
Thanked 182 Times in 173 Posts
Thanks much for the response.
Wot does the 1 do in the below code
Code:
awk '{$3="";sub("  "," ")}1

Sponsored Links
    #5  
Old 09-02-2010
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 October 2014, 7:38 AM EDT
Location: The Netherlands
Posts: 7,719
Thanks: 123
Thanked 538 Times in 508 Posts
Quote:
Originally Posted by michaelrozar17 View Post
Thanks much for the response.
Wot does the 1 do in the below code
Code:
awk '{$3="";sub("  "," ")}1

Conditions in awk control the execution of actions and actions are executed when the condition is true.
If the condition is true (1 is true) and there are no actions between braces, awk prints the current record by default.
This:

Code:
awk '{$3="";sub("  "," ")}1'

is similar to:

Code:
awk '{$3="";sub("  "," ")}{print}'

The Following User Says Thank You to Franklin52 For This Useful Post:
cola (09-03-2010)
Sponsored Links
    #6  
Old 09-03-2010
cola cola is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 6 October 2014, 9:41 PM EDT
Location: Dhaka,Bangladesh
Posts: 185
Thanks: 38
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by michaelrozar17 View Post
Thanks much for the response.
Wot does the 1 do in the below code
Code:
awk '{$3="";sub("  "," ")}1

Read some awk tutorial.
That was easy.

---------- Post updated at 05:10 PM ---------- Previous update was at 05:08 PM ----------

Quote:
Originally Posted by Franklin52 View Post
Conditions in awk control the execution of actions and actions are executed when the condition is true.
If the condition is true (1 is true) and there are no actions between braces, awk prints the current record by default.
This:

Code:
awk '{$3="";sub("  "," ")}1'

is similar to:

Code:
awk '{$3="";sub("  "," ")}{print}'

How to do the same thing without using substitude function?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove a last field from a file sudharsan23 Shell Programming and Scripting 19 03-08-2010 03:30 AM
Using sed to remove lines where field is empty figaro Shell Programming and Scripting 4 09-01-2009 03:07 PM
Remove field starting with * and # sumit207 Shell Programming and Scripting 3 07-01-2009 05:16 AM
remove last field from string dddkiran Shell Programming and Scripting 2 02-09-2009 01:32 AM
remove space in front or end of each field happyv Shell Programming and Scripting 6 03-22-2007 02:05 AM



All times are GMT -4. The time now is 09:53 PM.