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!

How to replace append a prefix and suffix with sed/awk

Status
Not open for further replies.

keungn

IS-IT--Management
Apr 20, 2009
5
US
I've following lines in a file and I need to suffixed with "E" to the 2nd field if the length of 2nd field is greater then 7 digits.


For Example:
MTR16016002001000000000000000OA 2100670 000000000010000000100470000000NN NNE 000 E0000001


change to

MTR16016002001000000000000000OA 2100670E 000000000010000000100470000000NN NNE 000 E0000001

If the 2nd field is less then 7 digits in length, they need to be prefixed with a zero and suffixed with an E

For Example:
MTR16016003001000000000000000CA 2980 000000000590000005900000000000NN NNE 000 E0000001R

change to

MTR16016003001000000000000000CA 0002980E 000000000590000005900000000000NN NNE 000 E0000001R


Thanks for the pointers
 
Something like this ?
awk '{$2=sprintf("%07dE",$2)}1' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thx PHV.

I've attached the script and test file with your solution and getting an error.

awk:0602-533 Cannot find or open file MTR16016002001000000000000000OA.
 
Oops. forgot the attachment

Here's the script
#!/usr/bin/ksh
#set -x
MVRSFile=$1
MDMS_MVRS_Fileformat=$2

dateext=`date '+%Y%m%d%H%M%S'`
#echo $dateext > GoodOutput.log
#echo $dateext > BadOutput.log

while read line
do
# do somthing on each $line

# save each line in a buffer
##Radix_field=$(echo "$line")
echo "$line" > MVRS_line

# get first field for meter id from each line
First_prefix=`echo "$line" |cut -b -3`

# Check the first 3 char is start "MTR"
if [ $First_prefix = "MTR" ]; then
# process MTR line, check utility id has E
print "this is $First_prefix"

Meter_field=$(echo "${line}" | awk -v Utility_field=4 '{print $Utility_field}')
echo "${Meter_field}"
#Electric_meter=`echo "$Meter_field" | tail -c -1`
Electric_meter="${Meter_field##${Meter_field%%?}}"
echo "${Electric_meter}"
if [ $Electric_meter = "E" ]; then
print "Process meter_id, column 46-57 subtitution"

Meter_id=$(echo "${line}" | awk -v id_field=2 '{print $id_field}')

meter_length=0
#tmp_length=`echo ${#Meter_id}`
#meter_length=$((meter_length+tmp_length))
meter_length=$((meter_length+`echo ${#Meter_id}`))
echo $meter_length

if [[ $meter_length -lt 7 ]]; then
print "Need to prefix logic with a zero"

fi

print "Append E with Meter_id"
count=$((meter_length+46))
print $count
# Now replace the char in column $count by "E"
awk '{$2=sprintf("%07dE",$2)}1' ${line}
# awk -vr="${Meter_id}" '(sub("${Meter_id}${Electric_meter}",r))1' >> NEW_MVRSFILE
#
# awk 'BEGIN {FS=" "; OFS=" "}
# {
# sub( " ", "E", $count)
# }' $line

# sed 's/^\(.\{($count)\}\)" "/\"E"/' $line
#echo $meter_length
# echo ${line} >> NEW_MVRSFILE
exit
fi
fi

done <"$MVRSFile"
 
 http://www.mediafire.com/?jkiqujzmmzn
PH - your solution partially working. The prefix and suffix works

Here's the input
MTR16016002001000000000000000OA 2100670 000000000010000000100470000000NN NNE 000 E0000001

MTR16016002001000000000000000CA 909079 000000000290000002900570000000NN NNE 000 E0000001

Here's the output
MTR16016002001000000000000000OA 2100670E 000000000010000000100470000000NN NNE 000 E0000001

MTR16016002001000000000000000CA 0909079E 000000000290000002900570000000NN NNE 000 E0000001


As you see, it truncates the blanks for the first field. I need the 2nd field has to be started on column 46 which is the same as the input.

Any ideas...
 
Unfortunately a side effect of assigning a new value to a field number is that the white space in the original line is "compressed".

You can work around that by reconstructing the output line using something like this:

Code:
awk '{ [b]print[/b] [b]substr[/b]([blue]$0[/blue],1,45) [b]sprintf[/b]([red]"[/red][purple]%07dE[/purple][red]"[/red],[blue]$2[/blue]) [b]substr[/b]([blue]$0[/blue],54) }' inputfile

Annihilannic.
 
many thanks. This really helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top