Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditions with AWK

Status
Not open for further replies.

udaybo

Programmer
Jun 2, 2008
23
US
Hello all

I have a file with below data, I need to add a third column as XXXX if the first coloumn (H180620081) last number is "1"
and YYYY if the first coloumn last number is "2"

Can it be done with AWK, I would appreciate if anyone could help me ou with this.


H180620081 31310
H180620082 33330

result

H180620081 31310 XXXX
H180620082 33330 YYYY

Thank you
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PH,

I was trying something like below
(I am a Business Objects Developer and I am very new to scripting)

awk 'substr($1,length($1))="1"{$(NF+1)="XXXX"}' file

Thanks again
 
A starting point:
Code:
awk '{print $0 ($1~/1$/ ? " XXXX" : ($1~/2$/ ? " YYYY" : ""))}' file

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Another script regarding the same file

Script:awk 'NR%3 == 1 {s = $0}
NR%3 == 2 {print s > ("file_" ($1 ~ /1$/ ? 1 : 2))}
' file

I have a file (A.txt) with below data (it's not sample, i will always have five lines of data)
but they are timestamps and the second and fifth lines (H180620081 & H180620082) will always have their last numbers as 1 and 2.

So if it is 1 I would like to select the line above H180620081 and copied it into a different output file and if it is H180620082 then copied it into another file.

ccv01.fil.20080618134215.rep
H180620081 31310
space
ccv01.fil.20080618134236.rep
H180620082 33330


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top