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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substr, awk

Status
Not open for further replies.

ersatz

Programmer
Oct 29, 2002
114
US
Hi,
I have a file with 7 columns.
My 6th column contains a variable string.
I want to know the last 2 characters of this column.
Example of this field: F4REP_ACCOUNT_OUT_
or F4REP_ACCOUNT_OUT__DM.
IF the last 2 characters are DM then ………do something else….
Or if the last character is _ then…….. else……..
Can somebody help my?
Thanks in advance
 
# something to get you started
# working out the logic is left as an exercise.
{
 if (substr($6, length($6) -2) = "DM")
 doDMstuff
 else if (substr($6, length($6) -1) = "_")
 doUNDERstuff
 else
 doNONDMstuff
 
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi Vlad,

Thanks for your prompt response.
I am not a unix specialist but I need a script to resolve my problem. I know the algorithm but I don’t know to write this using awk in my script.
I want to have something like this:

If substr($6, length($6-1) =”_” then
TBL=$6$REG_TBL
Else
TBL=substr($6,1,18)$REG_TBL”_DM”
endif

Thanks again
 
You have some control characters in your post, but I think that's what you wanted:&#10;&#10;&#10;nawk -f ersatz.awk youTextFile.txt&#10;&#10;#------------- ersatz.awk&#10;{ &#10; TBL = (substr($6, length($6) -1) == &quot;”_”&quot;) ? $6 REG_TBL : substr($6,1,18) REG_TBL &quot;”_DM”&quot;&#10;&#10;}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
This simple piece of code worked by printing the line
numbers where field 6 ends with DM:
Code:
awk '
$6 ~/.*DM$/ {
        print &quot;PROCESS DM AT LINE &quot;, NR
        continue
}

{
print &quot;PROCESS NON DM AT LINE &quot;, NR
}'
[\code]
 
Hi,
REG_TBL=MMFSA
Like I sad, $6 is a column with variables values.
$6= F4REP_ACCOUNT_OUT_
Or
$6= F4REP_ACCOUNT_OUT__DM
So, if my value is F4REP_ACCOUNT_OUT_ , then
TBL=$6 & REG_TBL (concatenate)
If my value is F4REP_ACCOUNT_OUT__DM then
TBL= substr($6, 1,18) & REG_TBL & “_DM”
Thanks again.
PS My table is info_tbl.env

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top