AWK_Scripting_'If'_Statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK_Scripting_'If'_Statements
# 1  
Old 07-02-2012
AWK_Scripting_'If'_Statements

I am trying to read in a stacking velocity file with the following change in format midway through the file:

Code:
V2EU90-226 1293 5000 4100 
V2EU90227B 3103 170 1480

I want to output:
Code:
226 1293 5000 4100
227 3103 170 1480

How can I write an 'if' statement in AWK scripting to enable me to do this?

Many thanks for any comments

Last edited by Scrutinizer; 07-02-2012 at 03:45 PM.. Reason: code tags
# 2  
Old 07-02-2012
Quote:
Originally Posted by eknryan
V2EU90227B 3103 170 1480

I want to output:
227 3103 170 1480
How do you define the above change? What is the rule ?
Do you want to ignore "V2EU90" and take the numeric part till the end and ignore any non-numeric character for the first field?
Please elaborate.
# 3  
Old 07-02-2012
This arrow comes from the dark, laced with assumptions:

Code:
awk -F\- 'NF>1{print $2} NF==1{sub(/V2EU90/,"");sub(/[A-Za-z] /," ");print}' inputfile

This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 07-02-2012
check if this works for your file

Code:
$ awk '{a=substr($0,7)+0;sub("-","",a);$1=a}1' vel.txt 
226 1293 5000 4100
227 3103 170 1480

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 07-02-2012
Code:
sed -e '/.*-/s///; t' -e 's/.\{6\}//; s/. / /' file

Regards,
Alister
# 6  
Old 07-02-2012
Another possibility while we are waiting for the OP's clarification..
Code:
sed 's/[^0-9 ]//g; s/...//' infile

# 7  
Old 07-02-2012
Hi Elixir,

I understood this code but one question . what is the use of NF>1 and NF==1 here. could you please explain. thanks.
because without the NF field, i get the following

Code:
cat velocity | awk -F- '{print $2}; {sub(/V2EU90/,""); sub(/[A-za-z]/,""); print}'
226 1293 5000 4100
-226 1293 5000 4100 ## the first line is being worked on twice incorrectly. 

227 3103 170 1480

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question