Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Remove a field using AWK

Shell Programming and Scripting


Tags
remove awk, remove fields awk

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-02-2010
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 24 November 2014, 9:59 PM EST
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 Unix and Linux 09-02-2010
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 23 May 2015, 5:01 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,304
Thanks: 135
Thanked 468 Times in 422 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 Unix and Linux 09-02-2010
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 May 2015, 9:42 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 129
Thanked 544 Times in 512 Posts
To delete a specific column you can do something like:

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

    #4  
Old Unix and Linux 09-02-2010
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 24 November 2014, 9:59 PM EST
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 Unix and Linux 09-02-2010
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 23 May 2015, 9:42 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 129
Thanked 544 Times in 512 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 Unix and Linux 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 01:19 PM.